Skip to content

Commit

Permalink
[FELIX-6074][FELIX-6075] Support plain incremental manifest build, up…
Browse files Browse the repository at this point in the history
…grade to jdk 8

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1854547 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
gnodet committed Feb 28, 2019
1 parent 99a2fe7 commit e971b02
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 9 deletions.
17 changes: 15 additions & 2 deletions tools/maven-bundle-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</distributionManagement>

<properties>
<felix.java.version>7</felix.java.version>
<felix.java.version>8</felix.java.version>
<maven.site.path>bundle-plugin-archives/bundle-plugin-LATEST</maven.site.path>
</properties>

Expand Down Expand Up @@ -129,6 +129,14 @@
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<!--
<plugin>
<artifactId>maven-invoker-plugin</artifactId>
Expand Down Expand Up @@ -159,13 +167,18 @@
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.1</version>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>biz.aQute.bnd</groupId>
<artifactId>biz.aQute.bndlib</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.bundlerepository</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ protected MavenProject getProject()
return project;
}

protected Map<String, String> getInstructions() {
return instructions;
}

protected DependencyNode buildDependencyGraph( MavenProject mavenProject ) throws MojoExecutionException
{
DependencyNode dependencyGraph;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,40 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import java.util.jar.Manifest;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import aQute.bnd.osgi.Analyzer;
import aQute.bnd.osgi.Builder;
import aQute.bnd.osgi.Instructions;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Resource;
import aQute.lib.collections.ExtList;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
Expand All @@ -47,13 +70,6 @@
import org.osgi.service.metatype.MetaTypeService;
import org.sonatype.plexus.build.incremental.BuildContext;

import aQute.bnd.osgi.Analyzer;
import aQute.bnd.osgi.Builder;
import aQute.bnd.osgi.Instructions;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Resource;
import aQute.lib.collections.ExtList;


/**
* Generate an OSGi manifest for this project
Expand Down Expand Up @@ -85,6 +101,9 @@ protected void execute( MavenProject project, DependencyNode dependencyGraph, Ma
throws MojoExecutionException
{

if (supportIncrementalBuild && isUpToDate(project)) {
return;
}
// in incremental build execute manifest generation only when explicitly activated
// and when any java file was touched since last build
if (buildContext.isIncremental() && !(supportIncrementalBuild && anyJavaSourceFileTouchedSinceLastBuild())) {
Expand All @@ -96,6 +115,10 @@ protected void execute( MavenProject project, DependencyNode dependencyGraph, Ma
try
{
analyzer = getAnalyzer(project, dependencyGraph, instructions, properties, classpath);

if (supportIncrementalBuild) {
writeIncrementalInfo(project);
}
}
catch ( FileNotFoundException e )
{
Expand Down Expand Up @@ -329,6 +352,106 @@ protected Analyzer getAnalyzer( MavenProject project, DependencyNode dependencyG
return analyzer;
}

private void writeIncrementalInfo(MavenProject project) throws MojoExecutionException {
try {
Path cacheData = getIncrementalDataPath(project);
String curdata = getIncrementalData();
Files.createDirectories(cacheData.getParent());
try (Writer w = Files.newBufferedWriter(cacheData)) {
w.append(curdata);
}
} catch (IOException e) {
throw new MojoExecutionException("Error checking manifest uptodate status", e);
}
}

private boolean isUpToDate(MavenProject project) throws MojoExecutionException {
try {
Path cacheData = getIncrementalDataPath(project);
String prvdata;
if (Files.isRegularFile(cacheData)) {
prvdata = new String(Files.readAllBytes(cacheData), StandardCharsets.UTF_8);
} else {
prvdata = null;
}
String curdata = getIncrementalData();
if (curdata.equals(prvdata)) {
long lastmod = Files.getLastModifiedTime(cacheData).toMillis();
Set<String> stale = Stream.concat(Stream.of(new File(project.getBuild().getOutputDirectory())),
project.getArtifacts().stream().map(Artifact::getFile))
.flatMap(f -> newer(lastmod, f))
.collect(Collectors.toSet());
if (!stale.isEmpty()) {
getLog().info("Stale files: " + stale.stream()
.collect(Collectors.joining(", ")));
} else {
// everything is in order, skip
getLog().info("Skipping manifest generation, everything is up to date.");
return true;
}
} else {
if (prvdata == null) {
getLog().info("No previous run data found, generating manifest.");
} else {
getLog().info("Configuration changed, re-generating manifest.");
}
}
} catch (IOException e) {
throw new MojoExecutionException("Error checking manifest uptodate status", e);
}
return false;
}

private String getIncrementalData() {
return getInstructions().entrySet().stream().map(e -> e.getKey() + "=" + e.getValue())
.collect(Collectors.joining("\n", "", "\n"));
}

private Path getIncrementalDataPath(MavenProject project) {
return Paths.get(project.getBuild().getDirectory(), "maven-bundle-plugin",
"org.apache.felix_maven-bundle-plugin_manifest_xx");
}

private long lastmod(Path p) {
try {
return Files.getLastModifiedTime(p).toMillis();
} catch (IOException e) {
return 0;
}
}

private Stream<String> newer(long lastmod, File file) {
try {
if (file.isDirectory()) {
return Files.walk(file.toPath())
.filter(Files::isRegularFile)
.filter(p -> lastmod(p) > lastmod)
.map(Path::toString);
} else if (file.isFile()) {
if (lastmod(file.toPath()) > lastmod) {
if (file.getName().endsWith(".jar")) {
try (ZipFile zf = new ZipFile(file)) {
return zf.stream()
.filter(ze -> !ze.isDirectory())
.filter(ze -> ze.getLastModifiedTime().toMillis() > lastmod)
.map(ze -> file.toString() + "!" + ze.getName())
.collect(Collectors.toList())
.stream();
}
} else {
return Stream.of(file.toString());
}
} else {
return Stream.empty();
}
} else {
return Stream.empty();
}
} catch (IOException e) {
throw new IOError(e);
}
}


public static void writeManifest( Analyzer analyzer, File outputFile, boolean niceManifest,
boolean exportScr, File scrLocation, BuildContext buildContext, Log log ) throws Exception
Expand Down

0 comments on commit e971b02

Please sign in to comment.