This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
/
CopyJarsMojo.java
223 lines (203 loc) · 6.92 KB
/
CopyJarsMojo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* #%L
* ImageJ software for multidimensional image processing and analysis.
* %%
* Copyright (C) 2012 - 2015 Board of Regents of the University of
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
* Institute of Molecular Cell Biology and Genetics.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package net.imagej.maven;
import java.io.File;
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactCollector;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.dependency.tree.DependencyNode;
import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
import org.apache.maven.shared.dependency.tree.DependencyTreeBuilderException;
import org.apache.maven.shared.dependency.tree.traversal.CollectingDependencyNodeVisitor;
/**
* Copies .jar artifacts and their dependencies into an ImageJ.app/ directory
* structure.
*
* @author Johannes Schindelin
* @goal copy-jars
* @phase install
*/
public class CopyJarsMojo extends AbstractCopyJarsMojo {
/**
* The name of the property pointing to the ImageJ.app/ directory.
* <p>
* If no property of that name exists, or if it is not a directory, no .jar
* files are copied.
* </p>
*
* @parameter default-value="imagej.app.directory"
*/
private String imagejDirectoryProperty;
/**
* Whether to delete other versions when copying the files.
* <p>
* When copying a file and its dependencies to an ImageJ.app/ directory and
* there are other versions of the same file, we can warn or delete those
* other versions.
* </p>
*
* @parameter default-value="false" property="delete.other.versions"
*/
private boolean deleteOtherVersions;
/**
* @parameter property="project"
* @required
* @readonly
*/
private MavenProject project;
/**
* Session
*
* @parameter property="session"
*/
private MavenSession session;
/**
* List of Remote Repositories used by the resolver
*
* @parameter property="project.remoteArtifactRepositories"
* @readonly
* @required
*/
protected List<ArtifactRepository> remoteRepositories;
/**
* Location of the local repository.
*
* @parameter property="localRepository"
* @readonly
* @required
*/
protected ArtifactRepository localRepository;
/**
* @component role="org.apache.maven.artifact.metadata.ArtifactMetadataSource"
* hint="maven"
* @required
* @readonly
*/
private ArtifactMetadataSource artifactMetadataSource;
/**
* @component role="org.apache.maven.artifact.resolver.ArtifactCollector"
* @required
* @readonly
*/
private ArtifactCollector artifactCollector;
/**
* @component
* @required
* @readonly
*/
private DependencyTreeBuilder treeBuilder;
/**
* Used to look up Artifacts in the remote repository.
*
* @component
* @required
* @readonly
*/
protected ArtifactFactory artifactFactory;
/**
* Used to look up Artifacts in the remote repository.
*
* @component
* @required
* @readonly
*/
protected ArtifactResolver artifactResolver;
private File imagejDirectory;
@SuppressWarnings("unchecked")
public void execute() throws MojoExecutionException {
if (imagejDirectoryProperty == null) {
getLog()
.info(
"No property name for the ImageJ.app/ directory location was specified; Skipping");
return;
}
String path = System.getProperty(imagejDirectoryProperty);
if (path == null) path =
project.getProperties().getProperty(imagejDirectoryProperty);
if (path == null) {
if (hasIJ1Dependency(project)) getLog().info(
"Property '" + imagejDirectoryProperty + "' unset; Skipping copy-jars");
return;
}
final String interpolated = interpolate(path, project, session);
imagejDirectory = new File(interpolated);
if (!imagejDirectory.isDirectory()) {
getLog().warn(
"'" + imagejDirectory + "'" +
(interpolated.equals(path) ? "" : " (" + path + ")") +
" is not an ImageJ.app/ directory; Skipping copy-jars");
return;
}
try {
ArtifactFilter artifactFilter =
new ScopeArtifactFilter(Artifact.SCOPE_COMPILE);
DependencyNode rootNode =
treeBuilder.buildDependencyTree(project, localRepository,
artifactFactory, artifactMetadataSource, artifactFilter,
artifactCollector);
CollectingDependencyNodeVisitor visitor =
new CollectingDependencyNodeVisitor();
rootNode.accept(visitor);
for (final DependencyNode dependencyNode : (List<DependencyNode>) visitor
.getNodes())
{
if (dependencyNode.getState() == DependencyNode.INCLUDED) {
final Artifact artifact = dependencyNode.getArtifact();
final String scope = artifact.getScope();
if (scope != null && !scope.equals(Artifact.SCOPE_COMPILE) &&
!scope.equals(Artifact.SCOPE_RUNTIME)) continue;
try {
installArtifact(artifact, imagejDirectory, false,
deleteOtherVersions, artifactResolver, remoteRepositories,
localRepository);
}
catch (Exception e) {
throw new MojoExecutionException("Could not copy " + artifact +
" to " + imagejDirectory, e);
}
}
}
}
catch (DependencyTreeBuilderException e) {
throw new MojoExecutionException("Could not get the dependencies for " +
project.getArtifactId(), e);
}
}
}