|
| 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 | +} |
0 commit comments