1 package org.apache.maven.plugin.coreit;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.factory.ArtifactFactory;
24 import org.apache.maven.artifact.repository.ArtifactRepository;
25 import org.apache.maven.artifact.resolver.ArtifactResolver;
26 import org.apache.maven.model.Dependency;
27 import org.apache.maven.plugin.AbstractMojo;
28 import org.apache.maven.plugin.MojoExecutionException;
29
30 import java.io.File;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.util.List;
34 import java.util.Properties;
35
36 /**
37 * Resolves user-specified artifacts. This mimics in part the Maven Dependency Plugin and the Maven Surefire Plugin.
38 *
39 * @author Benjamin Bentmann
40 *
41 * @goal resolve
42 */
43 public class ResolveMojo
44 extends AbstractMojo
45 {
46
47 /**
48 * The local repository.
49 *
50 * @parameter default-value="${localRepository}"
51 * @readonly
52 * @required
53 */
54 private ArtifactRepository localRepository;
55
56 /**
57 * The remote repositories of the current Maven project.
58 *
59 * @parameter default-value="${project.remoteArtifactRepositories}"
60 * @readonly
61 * @required
62 */
63 private List remoteRepositories;
64
65 /**
66 * The artifact resolver.
67 *
68 * @component
69 */
70 private ArtifactResolver resolver;
71
72 /**
73 * The artifact factory.
74 *
75 * @component
76 */
77 private ArtifactFactory factory;
78
79 /**
80 * The dependencies to resolve.
81 *
82 * @parameter
83 */
84 private Dependency[] dependencies;
85
86 /**
87 * The path to a properties file to store the resolved artifact paths in.
88 *
89 * @parameter
90 */
91 private File propertiesFile;
92
93 /**
94 * Runs this mojo.
95 *
96 * @throws MojoExecutionException If the artifact could not be resolved
97 */
98 public void execute()
99 throws MojoExecutionException
100 {
101 getLog().info( "[MAVEN-CORE-IT-LOG] Resolving artifacts" );
102
103 Properties props = new Properties();
104
105 try
106 {
107 if ( dependencies != null )
108 {
109 for ( Dependency dependency : dependencies )
110 {
111 Artifact artifact =
112 factory.createArtifactWithClassifier( dependency.getGroupId(), dependency.getArtifactId(),
113 dependency.getVersion(), dependency.getType(),
114 dependency.getClassifier() );
115
116 getLog().info( "[MAVEN-CORE-IT-LOG] Resolving " + getId( artifact ) );
117
118 resolver.resolve( artifact, remoteRepositories, localRepository );
119
120 if ( artifact.getFile() != null )
121 {
122 props.setProperty( getId( artifact ), artifact.getFile().getPath() );
123 }
124
125 getLog().info( "[MAVEN-CORE-IT-LOG] " + artifact.getFile() );
126 }
127 }
128 }
129 catch ( Exception e )
130 {
131 throw new MojoExecutionException( "Failed to resolve artifacts: " + e.getMessage(), e );
132 }
133
134 if ( propertiesFile != null )
135 {
136 getLog().info( "[MAVEN-CORE-IT-LOG] Creating properties file " + propertiesFile );
137
138 try
139 {
140 propertiesFile.getParentFile().mkdirs();
141
142 try ( FileOutputStream fos = new FileOutputStream( propertiesFile ) )
143 {
144 props.store( fos, "MAVEN-CORE-IT" );
145 }
146 }
147 catch ( IOException e )
148 {
149 throw new MojoExecutionException( "Failed to create properties file: " + e.getMessage(), e );
150 }
151 }
152 }
153
154 private String getId( Artifact artifact )
155 {
156 artifact.isSnapshot(); // decouple from MNG-2961
157 return artifact.getId();
158 }
159
160 }