|
5 | 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
6 | 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
7 | 7 | */ |
| 8 | + |
8 | 9 | package org.seedstack.maven; |
9 | 10 |
|
| 11 | +import static org.twdata.maven.mojoexecutor.MojoExecutor.artifactId; |
| 12 | +import static org.twdata.maven.mojoexecutor.MojoExecutor.configuration; |
| 13 | +import static org.twdata.maven.mojoexecutor.MojoExecutor.executeMojo; |
| 14 | +import static org.twdata.maven.mojoexecutor.MojoExecutor.executionEnvironment; |
| 15 | +import static org.twdata.maven.mojoexecutor.MojoExecutor.goal; |
| 16 | +import static org.twdata.maven.mojoexecutor.MojoExecutor.groupId; |
| 17 | +import static org.twdata.maven.mojoexecutor.MojoExecutor.plugin; |
| 18 | +import static org.twdata.maven.mojoexecutor.MojoExecutor.version; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.LinkedHashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.TreeSet; |
10 | 28 | import org.apache.maven.execution.MavenSession; |
11 | 29 | import org.apache.maven.model.Dependency; |
12 | 30 | import org.apache.maven.model.Profile; |
13 | | -import org.apache.maven.plugin.AbstractMojo; |
14 | 31 | import org.apache.maven.plugin.BuildPluginManager; |
15 | 32 | import org.apache.maven.plugin.MojoExecutionException; |
16 | 33 | import org.apache.maven.plugin.MojoFailureException; |
17 | 34 | import org.apache.maven.plugins.annotations.Component; |
18 | 35 | import org.apache.maven.plugins.annotations.Mojo; |
| 36 | +import org.apache.maven.plugins.annotations.Parameter; |
19 | 37 | import org.apache.maven.project.DefaultProjectBuildingRequest; |
20 | 38 | import org.apache.maven.project.MavenProject; |
21 | 39 | import org.apache.maven.project.ProjectBuilder; |
22 | 40 | import org.apache.maven.project.ProjectBuildingException; |
23 | 41 |
|
24 | | -import java.io.File; |
25 | | -import java.util.ArrayList; |
26 | | -import java.util.HashMap; |
27 | | -import java.util.LinkedHashMap; |
28 | | -import java.util.List; |
29 | | -import java.util.Map; |
30 | | -import java.util.Set; |
31 | | -import java.util.TreeSet; |
32 | | - |
33 | | -import static org.twdata.maven.mojoexecutor.MojoExecutor.artifactId; |
34 | | -import static org.twdata.maven.mojoexecutor.MojoExecutor.configuration; |
35 | | -import static org.twdata.maven.mojoexecutor.MojoExecutor.executeMojo; |
36 | | -import static org.twdata.maven.mojoexecutor.MojoExecutor.executionEnvironment; |
37 | | -import static org.twdata.maven.mojoexecutor.MojoExecutor.goal; |
38 | | -import static org.twdata.maven.mojoexecutor.MojoExecutor.groupId; |
39 | | -import static org.twdata.maven.mojoexecutor.MojoExecutor.plugin; |
40 | | -import static org.twdata.maven.mojoexecutor.MojoExecutor.version; |
41 | | - |
42 | 42 | /** |
43 | 43 | * Release the project simply by stripping the -SNAPSHOT part of the version. |
44 | 44 | * Useful in continuous delivery pipelines. |
|
47 | 47 | */ |
48 | 48 | @Mojo(name = "release", requiresProject = true, threadSafe = false, aggregator = true) |
49 | 49 | public class ReleaseMojo extends AbstractSeedStackMojo { |
50 | | - public static final String SNAPSHOT_SUFFIX = "-SNAPSHOT"; |
51 | | - |
52 | | - @Component |
53 | | - private MavenProject executionMavenProject; |
54 | | - |
55 | | - @Component |
| 50 | + private static final String SNAPSHOT_SUFFIX = "-SNAPSHOT"; |
| 51 | + @Parameter(defaultValue = "${project}", readonly = true) |
| 52 | + private MavenProject mavenProject; |
| 53 | + @Parameter(defaultValue = "${session}", readonly = true) |
56 | 54 | private MavenSession mavenSession; |
57 | | - |
58 | 55 | @Component |
59 | 56 | private BuildPluginManager buildPluginManager; |
60 | | - |
61 | 57 | @Component |
62 | 58 | private ProjectBuilder projectBuilder; |
63 | 59 |
|
64 | 60 | @Override |
65 | 61 | public void execute() throws MojoExecutionException, MojoFailureException { |
66 | | - String currentVersion = executionMavenProject.getVersion(); |
| 62 | + String currentVersion = mavenProject.getVersion(); |
67 | 63 |
|
68 | | - if (isProjectDirty(getReactorModules("", executionMavenProject))) { |
69 | | - throw new MojoFailureException("Cannot continue, a POM transformation is already in progress, commit and revert it before executing the release goal"); |
| 64 | + if (isProjectDirty(getReactorModules("", mavenProject))) { |
| 65 | + throw new MojoFailureException( |
| 66 | + "Cannot continue, a POM transformation is already in progress, commit and revert it before " + |
| 67 | + "executing the release goal"); |
70 | 68 | } |
71 | 69 |
|
72 | | - if (!executionMavenProject.equals(getLocalRoot(executionMavenProject))) { |
73 | | - throw new MojoFailureException("Cannot continue, release goal must be executed from the local project root"); |
| 70 | + if (!mavenProject.equals(getLocalRoot(mavenProject))) { |
| 71 | + throw new MojoFailureException("Cannot continue, release goal must be executed from the local project " + |
| 72 | + "root"); |
74 | 73 | } |
75 | 74 |
|
76 | 75 | if (!currentVersion.endsWith(SNAPSHOT_SUFFIX)) { |
77 | 76 | throw new MojoFailureException("Cannot continue, project version is not a SNAPSHOT"); |
78 | 77 | } |
79 | 78 |
|
80 | | - if (executionMavenProject.hasParent() && executionMavenProject.getParent().getVersion().endsWith(SNAPSHOT_SUFFIX)) { |
| 79 | + if (mavenProject.hasParent() && mavenProject.getParent() |
| 80 | + .getVersion() |
| 81 | + .endsWith(SNAPSHOT_SUFFIX)) { |
81 | 82 | throw new MojoFailureException("Cannot continue, parent project is still a SNAPSHOT"); |
82 | 83 | } |
83 | 84 |
|
84 | 85 | String newVersion = currentVersion.substring(0, currentVersion.length() - SNAPSHOT_SUFFIX.length()); |
85 | 86 | getLog().info("SNAPSHOT version found, setting release version to " + newVersion); |
86 | | - executionMavenProject.getProperties().setProperty("newVersion", newVersion); |
87 | | - executeVersionsPlugin("set", executionMavenProject); |
| 87 | + mavenProject.getProperties().setProperty("newVersion", newVersion); |
| 88 | + executeVersionsPlugin("set", mavenProject); |
88 | 89 |
|
89 | 90 | boolean shouldRevert = false; |
90 | 91 | List<MavenProject> transformedModules = new ArrayList<>(); |
91 | | - transformedModules.add(executionMavenProject); |
92 | | - transformedModules.addAll(getReactorModules("", executionMavenProject).values()); |
| 92 | + transformedModules.add(mavenProject); |
| 93 | + transformedModules.addAll(getReactorModules("", mavenProject).values()); |
93 | 94 | getLog().info("Checking transformed modules"); |
94 | 95 | for (MavenProject transformedModule : transformedModules) { |
95 | 96 | try { |
@@ -127,17 +128,22 @@ private void commitModule(MavenProject mavenProject) throws MojoExecutionExcepti |
127 | 128 | } |
128 | 129 |
|
129 | 130 | private void checkModule(MavenProject mavenProject) throws MojoFailureException { |
130 | | - Map<String, Dependency> snapshotDependencies = new HashMap<String, Dependency>(); |
| 131 | + Map<String, Dependency> snapshotDependencies = new HashMap<>(); |
131 | 132 | for (Dependency dependency : mavenProject.getDependencies()) { |
132 | 133 | if (dependency.getVersion().endsWith(SNAPSHOT_SUFFIX)) { |
133 | 134 | snapshotDependencies.put(mavenProject.getArtifactId(), dependency); |
134 | 135 | } |
135 | 136 | } |
136 | 137 |
|
137 | 138 | if (!snapshotDependencies.isEmpty()) { |
138 | | - StringBuilder sb = new StringBuilder("Cannot continue, there are still SNAPSHOT dependencies in the project:\n"); |
| 139 | + StringBuilder sb = new StringBuilder( |
| 140 | + "Cannot continue, there are still SNAPSHOT dependencies in the project:\n"); |
139 | 141 | for (Map.Entry<String, Dependency> dependencyEntry : snapshotDependencies.entrySet()) { |
140 | | - sb.append("\t* ").append(dependencyEntry.getKey()).append(": ").append(dependencyEntry.getValue().getManagementKey()).append("\n"); |
| 142 | + sb.append("\t* ") |
| 143 | + .append(dependencyEntry.getKey()) |
| 144 | + .append(": ") |
| 145 | + .append(dependencyEntry.getValue().getManagementKey()) |
| 146 | + .append("\n"); |
141 | 147 | } |
142 | 148 |
|
143 | 149 | throw new MojoFailureException(sb.toString()); |
@@ -180,8 +186,8 @@ private Map<String, MavenProject> getReactorModules(String path, MavenProject pr |
180 | 186 | path += '/'; |
181 | 187 | } |
182 | 188 |
|
183 | | - Map<String, MavenProject> result = new LinkedHashMap<String, MavenProject>(); |
184 | | - Map<String, MavenProject> childResults = new LinkedHashMap<String, MavenProject>(); |
| 189 | + Map<String, MavenProject> result = new LinkedHashMap<>(); |
| 190 | + Map<String, MavenProject> childResults = new LinkedHashMap<>(); |
185 | 191 |
|
186 | 192 | Set<String> childModules = getChildModules(project); |
187 | 193 | for (String moduleName : childModules) { |
@@ -211,9 +217,8 @@ private Map<String, MavenProject> getReactorModules(String path, MavenProject pr |
211 | 217 | return result; |
212 | 218 | } |
213 | 219 |
|
214 | | - |
215 | 220 | private Set<String> getChildModules(MavenProject mavenProject) { |
216 | | - Set<String> childModules = new TreeSet<String>(); |
| 221 | + Set<String> childModules = new TreeSet<>(); |
217 | 222 | childModules.addAll(mavenProject.getModules()); |
218 | 223 | for (Profile profile : mavenProject.getModel().getProfiles()) { |
219 | 224 | childModules.addAll(profile.getModules()); |
@@ -246,7 +251,7 @@ private MavenProject getLocalRoot(MavenProject project) { |
246 | 251 | private MavenProject buildProject(File moduleProjectFile) throws ProjectBuildingException { |
247 | 252 | DefaultProjectBuildingRequest request = new DefaultProjectBuildingRequest(); |
248 | 253 | request.setSystemProperties(System.getProperties()); |
249 | | - request.setRepositorySession(executionMavenProject.getProjectBuildingRequest().getRepositorySession()); |
| 254 | + request.setRepositorySession(mavenProject.getProjectBuildingRequest().getRepositorySession()); |
250 | 255 | return projectBuilder.build(moduleProjectFile, request).getProject(); |
251 | 256 | } |
252 | 257 | } |
0 commit comments