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.nio.file.Files;
22  import java.util.Collection;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Objects;
26  import java.util.stream.Collectors;
27  
28  import org.apache.maven.api.Artifact;
29  import org.apache.maven.api.MojoExecution;
30  import org.apache.maven.api.ProducedArtifact;
31  import org.apache.maven.api.Project;
32  import org.apache.maven.api.Session;
33  import org.apache.maven.api.di.Inject;
34  import org.apache.maven.api.model.Plugin;
35  import org.apache.maven.api.plugin.Log;
36  import org.apache.maven.api.plugin.MojoException;
37  import org.apache.maven.api.plugin.annotations.Mojo;
38  import org.apache.maven.api.plugin.annotations.Parameter;
39  import org.apache.maven.api.services.ArtifactInstaller;
40  import org.apache.maven.api.services.ArtifactInstallerRequest;
41  import org.apache.maven.api.services.ArtifactManager;
42  import org.apache.maven.api.services.ProjectManager;
43  
44  /**
45   * Installs the project's main artifact, and any other artifacts attached by other plugins in the lifecycle, to the
46   * local repository.
47   */
48  @SuppressWarnings("unused")
49  @Mojo(name = "install", defaultPhase = "install")
50  public class InstallMojo implements org.apache.maven.api.plugin.Mojo {
51      @Inject
52      private Log log;
53  
54      @Inject
55      private Session session;
56  
57      @Inject
58      private Project project;
59  
60      @Inject
61      private MojoExecution mojoExecution;
62  
63      /**
64       * Whether every project should be installed during its own install-phase or at the end of the multimodule build. If
65       * set to {@code true} and the build fails, none of the reactor projects is installed.
66       * <strong>(experimental)</strong>
67       *
68       * @since 2.5
69       */
70      @Parameter(property = "installAtEnd", defaultValue = "true")
71      private boolean installAtEnd;
72  
73      /**
74       * Set this to <code>true</code> to bypass artifact installation. Use this for artifacts that do not need to be
75       * installed in the local repository.
76       *
77       * @since 2.4
78       */
79      @Parameter(property = "maven.install.skip", defaultValue = "false")
80      private boolean skip;
81  
82      /**
83       * Set this to <code>true</code> to allow incomplete project processing. By default, such projects are forbidden
84       * and Mojo will fail to process them. Incomplete project is a Maven Project that has any other packaging than
85       * "pom" and has no main artifact packaged. In the majority of cases, what user really wants here is a project
86       * with "pom" packaging and some classified artifact attached (typical example is some assembly being packaged
87       * and attached with classifier).
88       *
89       * @since 3.1.1
90       */
91      @Parameter(property = "allowIncompleteProjects", defaultValue = "false")
92      private boolean allowIncompleteProjects;
93  
94      private enum State {
95          SKIPPED,
96          INSTALLED,
97          TO_BE_INSTALLED
98      }
99  
100     private static final String INSTALL_PROCESSED_MARKER = InstallMojo.class.getName() + ".processed";
101 
102     public InstallMojo() {}
103 
104     private void putState(State state) {
105         session.getPluginContext(project).put(INSTALL_PROCESSED_MARKER, state.name());
106     }
107 
108     private void putState(State state, ArtifactInstallerRequest request) {
109         session.getPluginContext(project).put(INSTALL_PROCESSED_MARKER, state.name());
110         session.getPluginContext(project).put(ArtifactInstallerRequest.class.getName(), request);
111     }
112 
113     private State getState(Project project) {
114         Map<String, Object> pluginContext = session.getPluginContext(project);
115         return State.valueOf((String) pluginContext.get(INSTALL_PROCESSED_MARKER));
116     }
117 
118     private boolean hasState(Project project) {
119         Map<String, Object> pluginContext = session.getPluginContext(project);
120         return pluginContext.containsKey(INSTALL_PROCESSED_MARKER);
121     }
122 
123     private boolean usingPlugin(Project project) {
124         Plugin plugin = project.getBuild().getPluginsAsMap().get("org.apache.maven.plugins:maven-install-plugin");
125         return plugin != null
126                 && plugin.getExecutions().stream()
127                         .anyMatch(e -> Objects.equals(e.getId(), mojoExecution.getExecutionId())
128                                 && !"none".equals(e.getPhase()));
129     }
130 
131     @Override
132     public void execute() {
133         if (skip) {
134             log.info("Skipping artifact installation");
135             putState(State.SKIPPED);
136         } else {
137             if (!installAtEnd) {
138                 installProject(processProject(project));
139                 putState(State.INSTALLED);
140             } else {
141                 getLog().info("Deferring install for " + project.getGroupId() + ":" + project.getArtifactId() + ":"
142                         + project.getVersion() + " at end");
143                 putState(State.TO_BE_INSTALLED, processProject(project));
144             }
145         }
146 
147         List<Project> projectsUsingPlugin =
148                 session.getProjects().stream().filter(this::usingPlugin).collect(Collectors.toList());
149         if (allProjectsMarked(projectsUsingPlugin)) {
150             for (Project reactorProject : projectsUsingPlugin) {
151                 State state = getState(reactorProject);
152                 if (state == State.TO_BE_INSTALLED) {
153                     Map<String, Object> pluginContext = session.getPluginContext(reactorProject);
154                     ArtifactInstallerRequest request =
155                             (ArtifactInstallerRequest) pluginContext.get(ArtifactInstallerRequest.class.getName());
156                     installProject(request);
157                 }
158             }
159         }
160     }
161 
162     private boolean allProjectsMarked(List<Project> projectsUsingPlugin) {
163         return projectsUsingPlugin.stream().allMatch(this::hasState);
164     }
165 
166     private void installProject(ArtifactInstallerRequest request) {
167         try {
168             getArtifactInstaller().install(request);
169         } catch (MojoException e) {
170             throw e;
171         } catch (Exception e) {
172             throw new MojoException(e.getMessage(), e);
173         }
174     }
175 
176     /**
177      * Processes passed in {@link Project} and produces {@link ArtifactInstallerRequest} out of it.
178      *
179      * @throws IllegalArgumentException if project is badly set up.
180      */
181     private ArtifactInstallerRequest processProject(Project project) {
182         ProjectManager projectManager = getProjectManager();
183         Collection<ProducedArtifact> installables = projectManager.getAllArtifacts(project);
184         Collection<ProducedArtifact> attachedArtifacts = projectManager.getAttachedArtifacts(project);
185 
186         getArtifactManager().setPath(project.getPomArtifact(), project.getPomPath());
187 
188         for (Artifact installable : installables) {
189             if (!isValidPath(installable)) {
190                 if (installable == project.getMainArtifact().orElse(null)) {
191                     if (attachedArtifacts.isEmpty()) {
192                         throw new MojoException(
193                                 "The packaging for this project did not assign a file to the build artifact");
194                     } else {
195                         if (allowIncompleteProjects) {
196                             getLog().warn("");
197                             getLog().warn("The packaging plugin for this project did not assign");
198                             getLog().warn(
199                                             "a main file to the project but it has attachments. Change packaging to 'pom'.");
200                             getLog().warn("");
201                             getLog().warn("Incomplete projects like this will fail in future Maven versions!");
202                             getLog().warn("");
203                         } else {
204                             throw new MojoException("The packaging plugin for this project did not assign "
205                                     + "a main file to the project but it has attachments. Change packaging to 'pom'.");
206                         }
207                     }
208                 } else {
209                     throw new MojoException("The packaging for this project did not assign "
210                             + "a file to the attached artifact: " + installable);
211                 }
212             }
213         }
214 
215         return ArtifactInstallerRequest.build(session, (Collection) installables);
216     }
217 
218     private boolean isValidPath(Artifact a) {
219         return getArtifactManager().getPath(a).filter(Files::isRegularFile).isPresent();
220     }
221 
222     void setSkip(boolean skip) {
223         this.skip = skip;
224     }
225 
226     protected Log getLog() {
227         return log;
228     }
229 
230     private ArtifactInstaller getArtifactInstaller() {
231         return session.getService(ArtifactInstaller.class);
232     }
233 
234     private ArtifactManager getArtifactManager() {
235         return session.getService(ArtifactManager.class);
236     }
237 
238     private ProjectManager getProjectManager() {
239         return session.getService(ProjectManager.class);
240     }
241 }