View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.install;
20  
21  import java.io.File;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.function.Predicate;
25  import java.util.stream.Collectors;
26  
27  import org.apache.maven.RepositoryUtils;
28  import org.apache.maven.execution.MavenSession;
29  import org.apache.maven.model.Plugin;
30  import org.apache.maven.model.PluginExecution;
31  import org.apache.maven.plugin.AbstractMojo;
32  import org.apache.maven.plugin.MojoExecutionException;
33  import org.apache.maven.plugin.descriptor.PluginDescriptor;
34  import org.apache.maven.plugins.annotations.Component;
35  import org.apache.maven.plugins.annotations.LifecyclePhase;
36  import org.apache.maven.plugins.annotations.Mojo;
37  import org.apache.maven.plugins.annotations.Parameter;
38  import org.apache.maven.project.MavenProject;
39  import org.apache.maven.project.artifact.ProjectArtifact;
40  import org.eclipse.aether.RepositorySystem;
41  import org.eclipse.aether.artifact.Artifact;
42  import org.eclipse.aether.installation.InstallRequest;
43  import org.eclipse.aether.installation.InstallationException;
44  import org.eclipse.aether.util.artifact.ArtifactIdUtils;
45  
46  /**
47   * Installs the project's main artifact, and any other artifacts attached by other plugins in the lifecycle, to the
48   * local repository.
49   *
50   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
51   */
52  @Mojo(name = "install", defaultPhase = LifecyclePhase.INSTALL, threadSafe = true)
53  public class InstallMojo extends AbstractMojo {
54      @Component
55      private RepositorySystem repositorySystem;
56  
57      @Parameter(defaultValue = "${session}", required = true, readonly = true)
58      private MavenSession session;
59  
60      @Parameter(defaultValue = "${project}", readonly = true, required = true)
61      private MavenProject project;
62  
63      @Parameter(defaultValue = "${plugin}", required = true, readonly = true)
64      private PluginDescriptor pluginDescriptor;
65  
66      /**
67       * Whether every project should be installed during its own install-phase or at the end of the multimodule build. If
68       * set to {@code true} and the build fails, none of the reactor projects is installed.
69       * <strong>(experimental)</strong>
70       *
71       * @since 2.5
72       */
73      @Parameter(defaultValue = "false", property = "installAtEnd")
74      private boolean installAtEnd;
75  
76      /**
77       * Set this to <code>true</code> to bypass artifact installation. Use this for artifacts that do not need to be
78       * installed in the local repository.
79       *
80       * @since 2.4
81       */
82      @Parameter(property = "maven.install.skip", defaultValue = "false")
83      private boolean skip;
84  
85      /**
86       * Set this to <code>true</code> to allow incomplete project processing. By default, such projects are forbidden
87       * and Mojo will fail to process them. Incomplete project is a Maven Project that has any other packaging than
88       * "pom" and has no main artifact packaged. In the majority of cases, what user really wants here is a project
89       * with "pom" packaging and some classified artifact attached (typical example is some assembly being packaged
90       * and attached with classifier).
91       *
92       * @since 3.1.1
93       */
94      @Parameter(defaultValue = "false", property = "allowIncompleteProjects")
95      private boolean allowIncompleteProjects;
96  
97      private enum State {
98          SKIPPED,
99          INSTALLED,
100         TO_BE_INSTALLED
101     }
102 
103     private static final String INSTALL_PROCESSED_MARKER = InstallMojo.class.getName() + ".processed";
104 
105     private void putState(State state) {
106         getPluginContext().put(INSTALL_PROCESSED_MARKER, state.name());
107     }
108 
109     private State getState(MavenProject project) {
110         Map<String, Object> pluginContext = session.getPluginContext(pluginDescriptor, project);
111         return State.valueOf((String) pluginContext.get(INSTALL_PROCESSED_MARKER));
112     }
113 
114     private boolean hasState(MavenProject project) {
115         Map<String, Object> pluginContext = session.getPluginContext(pluginDescriptor, project);
116         return pluginContext.containsKey(INSTALL_PROCESSED_MARKER);
117     }
118 
119     @Override
120     public void execute() throws MojoExecutionException {
121         if (skip) {
122             getLog().info("Skipping artifact installation");
123             putState(State.SKIPPED);
124         } else {
125             if (installAtEnd) {
126                 getLog().info("Deferring install for " + project.getGroupId() + ":" + project.getArtifactId() + ":"
127                         + project.getVersion() + " at end");
128                 putState(State.TO_BE_INSTALLED);
129             } else {
130                 InstallRequest request = new InstallRequest();
131                 processProject(project, request);
132                 installProject(request);
133                 putState(State.INSTALLED);
134             }
135         }
136 
137         List<MavenProject> allProjectsUsingPlugin = getAllProjectsUsingPlugin();
138 
139         if (allProjectsMarked(allProjectsUsingPlugin)) {
140             InstallRequest request = new InstallRequest();
141             for (MavenProject reactorProject : allProjectsUsingPlugin) {
142                 State state = getState(reactorProject);
143                 if (state == State.TO_BE_INSTALLED) {
144                     processProject(reactorProject, request);
145                 }
146             }
147             installProject(request);
148         }
149     }
150 
151     private boolean allProjectsMarked(List<MavenProject> allProjectsUsingPlugin) {
152         return allProjectsUsingPlugin.stream().allMatch(this::hasState);
153     }
154 
155     private final Predicate<MavenProject> hasMavenInstallPluginExecution =
156             rp -> hasExecution(rp.getPlugin("org.apache.maven.plugins:maven-install-plugin"));
157 
158     private List<MavenProject> getAllProjectsUsingPlugin() {
159         return session.getProjects().stream()
160                 .filter(hasMavenInstallPluginExecution)
161                 .collect(Collectors.toList());
162     }
163 
164     private final Predicate<PluginExecution> havingGoals = pe -> !pe.getGoals().isEmpty();
165     private final Predicate<PluginExecution> nonePhase = pe -> !"none".equalsIgnoreCase(pe.getPhase());
166 
167     private boolean hasExecution(Plugin plugin) {
168         if (plugin == null) {
169             return false;
170         }
171 
172         return plugin.getExecutions().stream().filter(havingGoals).anyMatch(nonePhase);
173     }
174 
175     private void installProject(InstallRequest request) throws MojoExecutionException {
176         try {
177             repositorySystem.install(session.getRepositorySession(), request);
178         } catch (InstallationException e) {
179             throw new MojoExecutionException(e.getMessage(), e);
180         }
181     }
182 
183     /**
184      * Processes passed in {@link MavenProject} and prepares content of {@link InstallRequest} out of it.
185      *
186      * @throws MojoExecutionException if project is badly set up.
187      */
188     private void processProject(MavenProject project, InstallRequest request) throws MojoExecutionException {
189         // always exists, as project exists
190         Artifact pomArtifact = RepositoryUtils.toArtifact(new ProjectArtifact(project));
191         // always exists, but at "init" is w/o file (packaging plugin assigns file to this when packaged)
192         Artifact projectArtifact = RepositoryUtils.toArtifact(project.getArtifact());
193 
194         // pom project: pomArtifact and projectArtifact are SAME
195         // jar project: pomArtifact and projectArtifact are DIFFERENT
196         // incomplete project: is not pom project and projectArtifact has no file
197 
198         // we must compare coordinates ONLY (as projectArtifact may not have file, and Artifact.equals factors it in)
199         // BUT if projectArtifact has file set, use that one
200         if (ArtifactIdUtils.equalsId(pomArtifact, projectArtifact)) {
201             if (isFile(projectArtifact.getFile())) {
202                 pomArtifact = projectArtifact;
203             }
204             projectArtifact = null;
205         }
206 
207         if (isFile(pomArtifact.getFile())) {
208             request.addArtifact(pomArtifact);
209         } else {
210             throw new MojoExecutionException(
211                     "The POM for project " + project.getArtifactId() + " could not be attached");
212         }
213 
214         // is not packaged, is "incomplete"
215         boolean isIncomplete = projectArtifact != null && !isFile(projectArtifact.getFile());
216         if (projectArtifact != null) {
217             if (!isIncomplete) {
218                 request.addArtifact(projectArtifact);
219             } else if (!project.getAttachedArtifacts().isEmpty()) {
220                 if (allowIncompleteProjects) {
221                     getLog().warn("");
222                     getLog().warn("The packaging plugin for project " + project.getArtifactId() + " did not assign");
223                     getLog().warn("a main file to the project but it has attachments. Change packaging to 'pom'.");
224                     getLog().warn("");
225                     getLog().warn("Incomplete projects like this will fail in future Maven versions!");
226                     getLog().warn("");
227                 } else {
228                     throw new MojoExecutionException("The packaging plugin for project " + project.getArtifactId()
229                             + " did not assign a main file to the project but it has attachments. Change packaging"
230                             + " to 'pom'.");
231                 }
232             } else {
233                 throw new MojoExecutionException("The packaging plugin for project " + project.getArtifactId()
234                         + " did not assign a file to the build artifact");
235             }
236         }
237 
238         for (org.apache.maven.artifact.Artifact attached : project.getAttachedArtifacts()) {
239             getLog().debug("Attaching for install: " + attached.getId());
240             request.addArtifact(RepositoryUtils.toArtifact(attached));
241         }
242     }
243 
244     private boolean isFile(File file) {
245         return file != null && file.isFile();
246     }
247 }