Skip to content

Commit 5a6a161

Browse files
committed
Added mvnize:bootstrap goal to derive a data consumer pom from a data publisher pom.
1 parent f0d8af0 commit 5a6a161

File tree

11 files changed

+374
-217
lines changed

11 files changed

+374
-217
lines changed

mvnize-maven-plugin/src/main/java/org/aksw/maven/plugin/mvnize/MvnizeMojo.java

-132
This file was deleted.

mvnize-maven-plugin/src/main/java/org/aksw/maven/plugin/mvnize/Xpp3DomUtils.java

-14
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package org.aksw.maven.plugin.mvnize.mojo;
2+
3+
import java.io.File;
4+
import java.util.List;
5+
import java.util.Set;
6+
import java.util.stream.Collectors;
7+
8+
import org.aksw.maven.plugin.mvnize.util.BuildHelperUtils;
9+
import org.aksw.maven.plugin.mvnize.util.PomUtils;
10+
import org.apache.maven.artifact.Artifact;
11+
import org.apache.maven.artifact.DefaultArtifact;
12+
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
13+
import org.apache.maven.model.Dependency;
14+
import org.apache.maven.model.Model;
15+
import org.apache.maven.model.Plugin;
16+
import org.apache.maven.plugin.AbstractMojo;
17+
import org.apache.maven.plugin.MojoExecutionException;
18+
import org.apache.maven.plugins.annotations.Mojo;
19+
import org.apache.maven.plugins.annotations.Parameter;
20+
import org.apache.maven.project.MavenProject;
21+
22+
/**
23+
* Based on a pom.xml generated by mvn mvnize:generate-pom,
24+
* generate a bootstrap-pom.xml file which adds all
25+
* artifacts to its dependencies section.
26+
*/
27+
@Mojo(name = "bootstrap", requiresProject = true)
28+
public class MvnizeMojoBootstrap extends AbstractMojo {
29+
@Parameter(defaultValue = "${project.file}", readonly = true)
30+
private File sourcePomFile;
31+
32+
@Parameter(property = "parentId", required = false)
33+
private String parentId;
34+
35+
@Parameter(property = "artifactId", required = false, defaultValue = "my-group:my-artifact:0.0.1-SNAPSHOT")
36+
private String artifactId;
37+
38+
/** The output file. */
39+
@Parameter(property = "output", required = false, defaultValue = "generated.pom.xml")
40+
private File targetPomFile;
41+
42+
/** The Maven project */
43+
@Parameter(defaultValue = "${project}", readonly = false, required = false)
44+
private MavenProject project;
45+
46+
@Override
47+
public void execute() throws MojoExecutionException {
48+
if (targetPomFile.equals(sourcePomFile)) {
49+
throw new RuntimeException("Target pom file must not be the project's pom!");
50+
}
51+
52+
// Artifact of the source pom from which to the dependencies
53+
Artifact targetArtifact = PomUtils.parseArtifact(artifactId);
54+
55+
// Use the given options to build the target artifact
56+
// Use defaults if omitted
57+
Artifact sourceArtifact = new DefaultArtifact(
58+
project.getGroupId(), project.getArtifactId(), project.getVersion(), null, project.getPackaging(), null, new DefaultArtifactHandler(project.getPackaging()));
59+
60+
Model targetModel;
61+
if (targetPomFile.exists()) {
62+
targetModel = PomUtils.loadExistingPom(targetPomFile);
63+
} else {
64+
targetModel = PomUtils.createNewPom(targetArtifact);
65+
}
66+
67+
BuildHelperUtils.addArtifact(targetModel, targetArtifact);
68+
69+
if (parentId != null) {
70+
Artifact parent = PomUtils.parseArtifact(parentId);
71+
PomUtils.setParent(targetModel, parent);
72+
}
73+
74+
Set<String> existingDepIds = targetModel.getDependencies().stream()
75+
.map(PomUtils::toArtifact)
76+
.map(PomUtils::toId)
77+
.collect(Collectors.toSet());
78+
79+
Plugin buildHelperPlugin = BuildHelperUtils.getPlugin(project.getBuild());
80+
List<Artifact> sourceAttachments = BuildHelperUtils.listArtifacts(buildHelperPlugin, sourceArtifact);
81+
List<Dependency> dependencies = sourceAttachments.stream()
82+
.filter(artifact -> !existingDepIds.contains(PomUtils.toId(artifact)))
83+
.map(PomUtils::toDependency)
84+
.toList();
85+
86+
targetModel.getDependencies().addAll(dependencies);
87+
88+
PomUtils.writePomFile(targetPomFile, targetModel);
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.aksw.maven.plugin.mvnize.mojo;
2+
3+
import java.io.File;
4+
5+
import org.aksw.maven.plugin.mvnize.util.BuildHelperUtils;
6+
import org.aksw.maven.plugin.mvnize.util.PomUtils;
7+
import org.apache.maven.artifact.Artifact;
8+
import org.apache.maven.model.Model;
9+
import org.apache.maven.plugin.AbstractMojo;
10+
import org.apache.maven.plugin.MojoExecutionException;
11+
import org.apache.maven.plugins.annotations.Mojo;
12+
import org.apache.maven.plugins.annotations.Parameter;
13+
import org.apache.maven.project.MavenProject;
14+
15+
@Mojo(name = "generate-pom", requiresProject = false)
16+
public class MvnizeMojoGeneratePom extends AbstractMojo {
17+
@Parameter(defaultValue = "${project.file}", readonly = true)
18+
private File pomFile;
19+
20+
@Parameter(property = "parentId", required = false)
21+
private String parentId;
22+
23+
@Parameter(property = "artifactId", required = false)
24+
private String artifactId;
25+
26+
@Parameter(property = "file", required = false)
27+
private File file;
28+
29+
/** The Maven project */
30+
@Parameter(defaultValue = "${project}", readonly = false, required = false)
31+
private MavenProject project;
32+
33+
@Override
34+
public void execute() throws MojoExecutionException {
35+
Artifact artifact = PomUtils.parseArtifact(artifactId);
36+
artifact.setFile(file);
37+
38+
if (pomFile == null) {
39+
pomFile = new File("pom.xml");
40+
}
41+
42+
Model model;
43+
if (pomFile.exists()) {
44+
model = PomUtils.loadExistingPom(pomFile);
45+
} else {
46+
model = PomUtils.createNewPom(artifact);
47+
BuildHelperUtils.addBuildHelperVersionProperty(model);
48+
}
49+
50+
if (parentId != null) {
51+
Artifact parent = PomUtils.parseArtifact(parentId);
52+
PomUtils.setParent(model, parent);
53+
}
54+
55+
BuildHelperUtils.addArtifact(model, artifact);
56+
57+
PomUtils.writePomFile(pomFile, model);
58+
getLog().info("pom.xml has been generated/updated successfully.");
59+
}
60+
}

mvnize-maven-plugin/src/main/java/org/aksw/maven/plugin/mvnize/BuildHelperUtils.java mvnize-maven-plugin/src/main/java/org/aksw/maven/plugin/mvnize/util/BuildHelperUtils.java

+51-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,49 @@
1-
package org.aksw.maven.plugin.mvnize;
1+
package org.aksw.maven.plugin.mvnize.util;
22

3+
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.List;
37
import java.util.Map;
48
import java.util.Objects;
9+
import java.util.Optional;
10+
import java.util.Properties;
11+
import java.util.stream.Collectors;
512

13+
import org.apache.maven.artifact.Artifact;
614
import org.apache.maven.model.Build;
15+
import org.apache.maven.model.Model;
716
import org.apache.maven.model.Plugin;
817
import org.apache.maven.model.PluginExecution;
918
import org.codehaus.plexus.util.xml.Xpp3Dom;
1019

11-
1220
public class BuildHelperUtils {
1321
public static final String GROUP_ID = "org.codehaus.mojo";
1422
public static final String ARTIFACT_ID = "build-helper-maven-plugin";
1523

1624
public static final String EXECUTION_ID = "attach-artifacts";
1725

26+
public static void addBuildHelperVersionProperty(Model model) {
27+
Properties properties = model.getProperties();
28+
if (!properties.contains("build-helper-maven-plugin.version")) {
29+
properties.setProperty("build-helper-maven-plugin.version", "3.3.0");
30+
}
31+
}
32+
33+
public static void addArtifact(Model model, Artifact artifact) {
34+
File file = artifact.getFile();
35+
if (file != null) {
36+
Build build = model.getBuild();
37+
if (build == null) {
38+
build = new Build();
39+
model.setBuild(build);
40+
}
41+
42+
Plugin plugin = BuildHelperUtils.getOrCreatePlugin(build);
43+
attachArtifact(plugin, file.toString(), artifact.getType(), artifact.getClassifier());
44+
}
45+
}
46+
1847
public static String getKey() {
1948
return createPlugin().getKey();
2049
}
@@ -39,9 +68,14 @@ public static Plugin createPlugin() {
3968
// return plugin;
4069
// }
4170

42-
public static Plugin getOrCreatePlugin(Build build) {
71+
public static Plugin getPlugin(Build build) {
4372
Map<String, Plugin> pluginsMap = build.getPluginsAsMap();
4473
Plugin plugin = pluginsMap.get(getKey());
74+
return plugin;
75+
}
76+
77+
public static Plugin getOrCreatePlugin(Build build) {
78+
Plugin plugin = getPlugin(build);
4579
if (plugin == null) {
4680
plugin = createPlugin();
4781
build.addPlugin(plugin);
@@ -97,6 +131,20 @@ public static Xpp3Dom attachArtifact(Plugin plugin, String file, String type, St
97131
return target;
98132
}
99133

134+
public static List<Artifact> listArtifacts(Plugin plugin, Artifact prototype) {
135+
Map<String, PluginExecution> executionsMap = plugin.getExecutionsAsMap();
136+
PluginExecution execution = executionsMap.get(EXECUTION_ID);
137+
List<Artifact> result = Optional.ofNullable(execution)
138+
.map(PluginExecution::getConfiguration)
139+
.map(conf -> (Xpp3Dom)conf)
140+
.map(conf -> conf.getChild("artifacts"))
141+
.stream()
142+
.flatMap(artifacts -> Arrays.asList(artifacts.getChildren()).stream())
143+
.map(node -> Xpp3DomUtils.extractArtifact(node, prototype))
144+
.collect(Collectors.toCollection(ArrayList::new));
145+
return result;
146+
}
147+
100148
// public static Xpp3Dom attachArtifact(Plugin plugin, String file, String type, String classifier) {
101149
// PluginExecution execution = plugin.getExecutionsAsMap().get("attach-artifacts");
102150
//

0 commit comments

Comments
 (0)