Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maven codegen Mojo #5319

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,134 @@
package aQute.bnd.maven.lib.configuration;

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 configurations for maven plugins consistently over the
* various Mojos.
*
* @author Juergen Albert
*/
public class Configurations {

protected final static Logger logger = LoggerFactory.getLogger(Configurations.class);

public static File loadProperties(Processor processor, MavenProject project, MojoExecution mojoExecution)
throws Exception {
// Load parent project properties first
loadParentProjectProperties(processor, project, mojoExecution);

// Load current project properties
Xpp3Dom configuration = Optional.ofNullable(project.getBuildPlugins())
.flatMap(new FlatMapHelper(mojoExecution)::getConfiguration)
.orElseGet(Configurations::defaultConfiguration);
return loadProjectProperties(processor, project, project, configuration);
}

private static void loadParentProjectProperties(Processor builder, MavenProject currentProject,
MojoExecution mojoExecution) throws Exception {
MavenProject parentProject = currentProject.getParent();
if (parentProject == null) {
return;
}
loadParentProjectProperties(builder, parentProject, mojoExecution);

// Get configuration from parent project
Xpp3Dom configuration = Optional.ofNullable(parentProject.getBuildPlugins())
.flatMap(new FlatMapHelper(mojoExecution)::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(new FlatMapHelper(mojoExecution)::getConfiguration)
.orElseGet(Configurations::defaultConfiguration);
// Load properties from parent project's bnd file or configuration in
// project's pluginManagement
loadProjectProperties(builder, parentProject, currentProject, configuration);
}

private static 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 static class FlatMapHelper {

private MojoExecution mojoExecution;

public FlatMapHelper(MojoExecution mojoExecution) {
this.mojoExecution = mojoExecution;
}

public 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 static Xpp3Dom defaultConfiguration() {
return new Xpp3Dom("configuration");
}

}
@@ -1,4 +1,4 @@
@Version("1.1.0")
@Version("1.2.0")
@Export
package aQute.bnd.maven.lib.configuration;

Expand Down
2 changes: 1 addition & 1 deletion biz.aQute.bndlib/src/aQute/bnd/build/ProjectGenerate.java
Expand Up @@ -92,7 +92,7 @@ private Result<Void> prepare(String sourceWithDuplicate, GeneratorSpec st) {

Set<File> sourceFiles = new FileSet(project.getBase(), source).getFiles();
if (sourceFiles.isEmpty())
return err("No source files/directories specified");
return err("No source files/directories found in fileset %s", source);

File out = project.getFile(output);
if (out.isDirectory()) {
Expand Down
5 changes: 5 additions & 0 deletions maven/README.md
Expand Up @@ -59,6 +59,10 @@ A plugin to run a bndrun file.

A plugin to generate and export reports of projects.

## [bnd-generate-maven-plugin][11]

A plugin to generate sources and resources.

# Building the Maven Plugins

You must first run `./gradlew :build` to build the Bnd artifacts and install them in your local maven repo.
Expand Down Expand Up @@ -113,3 +117,4 @@ pom's `pluginManagement` section, to configure the repository:
[8]: bnd-testing-maven-plugin/README.md
[9]: bnd-run-maven-plugin/README.md
[10]: bnd-reporter-maven-plugin/README.md
[11]: bnd-generate-maven-plugin/README.md
3 changes: 3 additions & 0 deletions maven/bnd-generate-maven-plugin/.gitignore
@@ -0,0 +1,3 @@
/target/
/bin/
/.classpath
23 changes: 23 additions & 0 deletions maven/bnd-generate-maven-plugin/.project
@@ -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>
@@ -0,0 +1,5 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
line.separator=\n