
net.imagej.maven.CopyJarsMojo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of imagej-maven-plugin Show documentation
Show all versions of imagej-maven-plugin Show documentation
A plugin helping with installing ImageJ 1.x plugins and dependencies. In ImageJ 1.x, .jar files containing an underscore are automatically scanned for classes whose names contain underscores. Every such class is considered to be a plugin. That is not true, however, for jai_codec.jar's classes. Therefore, Fiji introduced the split between plugins/ and jars/, where third-party libraries are expected to live in jars/. ImageJ2 uses the same structure since it is backwards compatible.
/*
* #%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.
*
* If no property of that name exists, or if it is not a directory, no .jar
* files are copied.
*
*
* @parameter default-value="imagej.app.directory"
*/
private String imagejDirectoryProperty;
/**
* Whether to delete other versions when copying the files.
*
* 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.
*
*
* @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 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) 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);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy