Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5385 from bjhargrave/maven_codegen
Maven codegen
- Loading branch information
Showing
56 changed files
with
2,257 additions
and
102 deletions.
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
biz.aQute.bnd.maven/src/aQute/bnd/maven/lib/configuration/BndConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package aQute.bnd.maven.lib.configuration; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import org.apache.maven.model.Plugin; | ||
import org.apache.maven.model.PluginExecution; | ||
import org.apache.maven.model.PluginManagement; | ||
import org.apache.maven.plugin.MojoExecution; | ||
import org.apache.maven.project.MavenProject; | ||
import org.codehaus.plexus.util.xml.Xpp3Dom; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import aQute.bnd.build.Project; | ||
import aQute.bnd.osgi.Processor; | ||
import aQute.lib.io.IO; | ||
import aQute.lib.utf8properties.UTF8Properties; | ||
|
||
/** | ||
* A helper to read Bnd configuration for maven plugins consistently over the | ||
* various Mojos. | ||
*/ | ||
public class BndConfiguration { | ||
private final static Logger logger = LoggerFactory.getLogger(BndConfiguration.class); | ||
|
||
private final MavenProject project; | ||
private final MojoExecution mojoExecution; | ||
|
||
public BndConfiguration(MavenProject project, MojoExecution mojoExecution) { | ||
this.project = requireNonNull(project); | ||
this.mojoExecution = requireNonNull(mojoExecution); | ||
} | ||
|
||
public File loadProperties(Processor processor) throws Exception { | ||
// Load parent project properties first | ||
loadParentProjectProperties(processor, project); | ||
|
||
// Load current project properties | ||
Xpp3Dom configuration = Optional.ofNullable(project.getBuildPlugins()) | ||
.flatMap(this::getConfiguration) | ||
.orElseGet(this::defaultConfiguration); | ||
return loadProjectProperties(processor, project, project, configuration); | ||
} | ||
|
||
private void loadParentProjectProperties(Processor builder, MavenProject currentProject) throws Exception { | ||
MavenProject parentProject = currentProject.getParent(); | ||
if (parentProject == null) { | ||
return; | ||
} | ||
loadParentProjectProperties(builder, parentProject); | ||
|
||
// Get configuration from parent project | ||
Xpp3Dom configuration = Optional.ofNullable(parentProject.getBuildPlugins()) | ||
.flatMap(this::getConfiguration) | ||
.orElse(null); | ||
if (configuration != null) { | ||
// Load parent project's properties | ||
loadProjectProperties(builder, parentProject, parentProject, configuration); | ||
return; | ||
} | ||
|
||
// Get configuration in project's pluginManagement | ||
configuration = Optional.ofNullable(currentProject.getPluginManagement()) | ||
.map(PluginManagement::getPlugins) | ||
.flatMap(this::getConfiguration) | ||
.orElseGet(this::defaultConfiguration); | ||
// Load properties from parent project's bnd file or configuration in | ||
// project's pluginManagement | ||
loadProjectProperties(builder, parentProject, currentProject, configuration); | ||
} | ||
|
||
private File loadProjectProperties(Processor processor, MavenProject bndProject, MavenProject pomProject, | ||
Xpp3Dom configuration) throws Exception { | ||
// check for bnd file configuration | ||
File baseDir = bndProject.getBasedir(); | ||
if (baseDir != null) { // file system based pom | ||
File pomFile = bndProject.getFile(); | ||
processor.updateModified(pomFile.lastModified(), "POM: " + pomFile); | ||
// check for bnd file | ||
Xpp3Dom bndfileElement = configuration.getChild("bndfile"); | ||
String bndFileName = (bndfileElement != null) ? bndfileElement.getValue() : Project.BNDFILE; | ||
File bndFile = IO.getFile(baseDir, bndFileName); | ||
if (bndFile.isFile()) { | ||
logger.debug("loading bnd properties from file: {}", bndFile); | ||
// we use setProperties to handle -include | ||
processor.setProperties(bndFile.getParentFile(), processor.loadProperties(bndFile)); | ||
return bndFile; | ||
} | ||
// no bnd file found, so we fall through | ||
} | ||
|
||
// check for bnd-in-pom configuration | ||
baseDir = pomProject.getBasedir(); | ||
File pomFile = pomProject.getFile(); | ||
if (baseDir != null) { | ||
processor.updateModified(pomFile.lastModified(), "POM: " + pomFile); | ||
} | ||
Xpp3Dom bndElement = configuration.getChild("bnd"); | ||
if (bndElement != null) { | ||
logger.debug("loading bnd properties from bnd element in pom: {}", pomProject); | ||
UTF8Properties properties = new UTF8Properties(); | ||
properties.load(bndElement.getValue(), pomFile, processor); | ||
// we use setProperties to handle -include | ||
processor.setProperties(baseDir, properties.replaceHere(baseDir)); | ||
} | ||
return pomFile; | ||
} | ||
|
||
private Optional<Xpp3Dom> getConfiguration(List<Plugin> plugins) { | ||
return plugins.stream() | ||
.filter(p -> Objects.equals(p, mojoExecution.getPlugin())) | ||
.map(Plugin::getExecutions) | ||
.flatMap(List::stream) | ||
.filter(e -> Objects.equals(e.getId(), mojoExecution.getExecutionId())) | ||
.findFirst() | ||
.map(PluginExecution::getConfiguration) | ||
.map(Xpp3Dom.class::cast) | ||
.map(Xpp3Dom::new); | ||
} | ||
|
||
private Xpp3Dom defaultConfiguration() { | ||
return new Xpp3Dom("configuration"); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
biz.aQute.bnd.maven/src/aQute/bnd/maven/lib/configuration/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
@Version("1.1.0") | ||
@Version("1.2.0") | ||
@Export | ||
package aQute.bnd.maven.lib.configuration; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/target/ | ||
/bin/ | ||
/.classpath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>bnd-generate-maven-plugin</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
<buildCommand> | ||
<name>org.eclipse.m2e.core.maven2Builder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
<nature>org.eclipse.m2e.core.maven2Nature</nature> | ||
</natures> | ||
</projectDescription> |
4 changes: 4 additions & 0 deletions
4
maven/bnd-generate-maven-plugin/.settings/org.eclipse.core.resources.prefs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
eclipse.preferences.version=1 | ||
encoding//src/main/java=UTF-8 | ||
encoding//src/main/resources=UTF-8 | ||
encoding/<project>=UTF-8 |
2 changes: 2 additions & 0 deletions
2
maven/bnd-generate-maven-plugin/.settings/org.eclipse.core.runtime.prefs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
eclipse.preferences.version=1 | ||
line.separator=\n |
Oops, something went wrong.