1 package org.apache.maven.plugin.coreit;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
38
39
40
41
42
43 public class ResolveMojo
44 extends AbstractMojo
45 {
46
47
48
49
50
51
52
53
54 private ArtifactRepository localRepository;
55
56
57
58
59
60
61
62
63 private List remoteRepositories;
64
65
66
67
68
69
70 private ArtifactResolver resolver;
71
72
73
74
75
76
77 private ArtifactFactory factory;
78
79
80
81
82
83
84 private Dependency[] dependencies;
85
86
87
88
89
90
91 private File propertiesFile;
92
93
94
95
96
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();
157 return artifact.getId();
158 }
159
160 }