
org.jboss.maven.plugins.retro.WeaveDependenciesMojo Maven / Gradle / Ivy
The newest version!
package org.jboss.maven.plugins.retro;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.deployer.ArtifactDeployer;
import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
import org.apache.maven.artifact.installer.ArtifactInstallationException;
import org.apache.maven.artifact.installer.ArtifactInstaller;
import org.apache.maven.artifact.metadata.ArtifactMetadata;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.metadata.ResolutionGroup;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
import org.apache.maven.artifact.repository.DefaultArtifactRepository;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.artifact.ProjectArtifactMetadata;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.StringUtils;
/**
* This Mojo can be used to weave project dependencies.
*
* @goal weave-dependencies
* @requiresDependencyResolution
*/
public class WeaveDependenciesMojo extends AbstractWeaveMojo
{
/**
* INTERNAL : Artifact resolver, needed to download dependencies
*
* @component role="org.apache.maven.artifact.resolver.ArtifactResolver"
* @required
* @readonly
*/
protected ArtifactResolver artifactResolver;
/**
* INTERNAL : Artifact resolver, needed to download dependencies
*
* @component role="org.apache.maven.artifact.metadata.ArtifactMetadataSource"
* @required
* @readonly
*/
protected ArtifactMetadataSource artifactMetadataSource;
/**
* INTERNAL : Local maven repository.
*
* @parameter expression="${localRepository}"
* @required
* @readonly
*/
protected ArtifactRepository localRepository;
/**
* The path for a specific local repository directory. It will wrap into an ArtifactRepository
* with localRepoId
as id
and with default repositoryLayout
*
* @parameter expression="${localRepositoryPath}"
*/
private File localRepositoryPath;
/**
* The id
for the localRepo
*
* @parameter expression="${localRepositoryId}"
*/
private String localRepositoryId;
/**
* Map that contains the layouts
*
* @component role="org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout"
*/
private Map repositoryLayouts;
/**
* A maven component to install artifacts to a local repository.
* @parameter expression="${component.org.apache.maven.artifact.installer.ArtifactInstaller}"
* @required
* @readonly
*/
protected ArtifactInstaller installer;
/**
* Maven component to deploy artifacts to remote repository.
* @parameter expression="${component.org.apache.maven.artifact.deployer.ArtifactDeployer}"
* @required
* @readonly
*/
private ArtifactDeployer deployer;
/**
* The plugin dependencies.
*
* @parameter expression="${plugin.artifacts}"
* @required
* @readonly
*/
protected List pluginArtifacts;
/**
* URL where the artifact will be deployed.
* ie ( file://C:\m2-repo or scp://host.com/path/to/repo )
* Note: this parameter is required if deployDependencies is set
* to true.
*
* @parameter
*
*/
private String deployUrl;
/**
* The type of remote repository layout to deploy to. Try legacy for
* a Maven 1.x-style repository layout.
*
* @parameter expression="${repositoryLayout}" default-value="default"
* @required
*/
private String repositoryLayout;
/**
* The directory where the weaved jar files should be written.
* should be written.
*
* @parameter expression="${project.build.directory}/weaved-dependencies"
*/
protected String outputDirectory;
/**
* Component used to create a repository
*
* @component
*/
private ArtifactRepositoryFactory repositoryFactory;
/**
* Server Id to map on the <id> under <server> section of settings.xml
* In most cases, this parameter will be required for authentication.
*
* @parameter expression="${repositoryId}" default-value="remote-repository"
* @required
*/
private String repositoryId;
/**
* Whether to deploy snapshots with a unique version or not.
*
* @parameter expression="${uniqueVersion}" default-value="true"
*/
private boolean uniqueVersion;
/**
* Deploy weaved dependencies to a repository.
* @parameter default-value="false"
*/
private boolean deployDependencies;
/**
* Install weaved dependencies to a local repository.
* @parameter default-value="false"
*
*/
private boolean installDependencies;
/**
* Main plugin execution method
*/
public void execute() throws MojoFailureException, MojoExecutionException
{
// Override the default local repository
if ( StringUtils.isNotEmpty( localRepositoryId ) && ( localRepositoryPath != null ) )
{
try
{
ArtifactRepositoryLayout layout;
layout = ( ArtifactRepositoryLayout ) repositoryLayouts.get( repositoryLayout );
getLog().info("Layout: " + layout.getClass());
localRepository = new DefaultArtifactRepository( localRepositoryId, localRepositoryPath.toURL()
.toString(), layout );
}
catch ( MalformedURLException e )
{
throw new MojoExecutionException( "MalformedURLException: " + e.getMessage(), e );
}
}
this.getLog().info("Weaving dependencies");
weaveDependencies();
this.getLog().info("Weaving complete.");
}
protected void weaveDependencies() throws MojoFailureException, MojoExecutionException
{
Set dependencyArtifacts = project.getDependencyArtifacts();
Map weavedDependencies = new HashMap();
try
{
ArtifactResolutionResult result = artifactResolver.resolveTransitively(dependencyArtifacts,
project.getArtifact(), project.getRemoteArtifactRepositories(), localRepository, artifactMetadataSource);
Set artifacts = result.getArtifacts();
File parentDir = new File(this.getOutputPath());
if ( ! parentDir.exists()) {
parentDir.mkdirs();
}
for (Object artifactObj : artifacts)
{
Artifact artifact = (Artifact)artifactObj;
getLog().info("Weaving " + artifact);
String srcJar = artifact.getFile().getAbsolutePath();
File outputJarFile = new File(this.getOutputPath() + File.separator + artifact.getFile().getName());
String destJar = outputJarFile.getAbsolutePath();
String classpath = buildClasspath(artifacts);
String [] args = this.generateWeaverArgs(classpath, srcJar, destJar);
try
{
doWeave(classpath, args);
}
catch (Exception e)
{
getLog().warn("Exception while weaving " + artifact.getArtifactId() + "\n" +
e.getMessage());
}
weavedDependencies.put(artifact, outputJarFile);
}
if (this.installDependencies)
{
this.installDependencies(weavedDependencies);
}
if (this.deployDependencies)
{
deployDependencies(weavedDependencies);
}
}
catch (ArtifactResolutionException e)
{
throw new MojoExecutionException(e.getMessage(), e);
}
catch (ArtifactNotFoundException e)
{
throw new MojoExecutionException(e.getMessage(), e);
}
catch (ArtifactInstallationException e)
{
throw new MojoExecutionException(e.getMessage(), e);
}
}
protected String [] generateWeaverArgs(String classpath, String srcJar, String destJar)
{
ArrayList argsList = new ArrayList ();
if (this.verbose)
{
argsList.add("-verbose");
}
if (this.suppress)
{
argsList.add("-suppress");
}
argsList.add("-cp");
argsList.add(classpath);
argsList.add("-useSystemClasspath");
argsList.add("false");
if (this.weaverClass !=null)
{
argsList.add("-weaverClass");
argsList.add(this.weaverClass);
}
argsList.add("-outputJar");
argsList.add(destJar);
argsList.add(srcJar);
String [] args = new String[argsList.size()];
return argsList.toArray(args);
}
/**
* Generates a classpath string based on the resolved dependencies.
* @return The classpath string
*/
protected String buildClasspath(Set artifacts) {
StringBuilder classpath = new StringBuilder();
for (Object artifactObj : artifacts)
{
Artifact artifact = (Artifact)artifactObj;
classpath.append(artifact.getFile().getAbsolutePath());
classpath.append(pathSep);
}
for (Object artifactObj : pluginArtifacts)
{
try
{
Artifact artifact = (Artifact) artifactObj;
if (artifact.getFile() != null)
{
classpath.append(artifact.getFile().getCanonicalPath());
classpath.append(pathSep);
}
}
catch (IOException ioe)
{
this.getLog().warn("Could not get filename");
}
}
return classpath.toString();
}
protected File getPomFile(Artifact artifact) throws MojoExecutionException
{
Artifact pomArtifact = null;
try
{
ResolutionGroup resGroup = artifactMetadataSource.retrieve(artifact, localRepository, project
.getRemoteArtifactRepositories());
pomArtifact = resGroup.getPomArtifact();
artifactResolver.resolve(pomArtifact, project.getRemoteArtifactRepositories(), localRepository);
}
catch (ArtifactMetadataRetrievalException e)
{
throw new MojoExecutionException( e.getMessage(), e);
}
catch (ArtifactResolutionException e)
{
throw new MojoExecutionException( e.getMessage(), e);
}
catch (ArtifactNotFoundException e)
{
throw new MojoExecutionException( e.getMessage(), e);
}
if (pomArtifact == null)
{
return null;
}
else
{
return pomArtifact.getFile();
}
}
private void installDependencies(Map artifacts) throws MojoExecutionException, ArtifactInstallationException
{
for (Object artifactObj : artifacts.keySet())
{
Artifact artifact = (Artifact)artifactObj;
File weavedArtifactFile = (File)artifacts.get(artifact);
attachPomMetadata(artifact);
installer.install(weavedArtifactFile, artifact, localRepository);
}
}
private void deployDependencies(Map artifacts) throws MojoExecutionException
{
getLog().info("Deploying dependencies");
ArtifactRepositoryLayout layout = ( ArtifactRepositoryLayout ) repositoryLayouts.get( repositoryLayout );
if (deployUrl == null)
{
throw new MojoExecutionException("Parameter deployUrl must be set to a valid URL when deploying artifacts");
}
ArtifactRepository deploymentRepository =
repositoryFactory.createDeploymentArtifactRepository( repositoryId, deployUrl, layout, uniqueVersion );
String protocol = deploymentRepository.getProtocol();
if ( protocol.equals( "" ) || protocol == null )
{
throw new MojoExecutionException( "No transfer protocol found." );
}
for (Object artifactObj : artifacts.keySet())
{
Artifact artifact = (Artifact)artifactObj;
File weavedArtifactFile = (File)artifacts.get(artifact);
attachPomMetadata(artifact);
try
{
getDeployer().deploy( weavedArtifactFile, artifact, deploymentRepository, getLocalRepository() );
}
catch ( ArtifactDeploymentException e )
{
throw new MojoExecutionException( e.getMessage(), e );
}
}
}
private void attachPomMetadata(Artifact artifact) throws MojoExecutionException
{
File pomFile = getPomFile(artifact);
File retroPomFile = new File(this.getOutputPath() + File.separator + artifact.getArtifactId() + "-" + artifact.getVersion() + ".pom");
try
{
FileUtils.copyFile(pomFile, retroPomFile);
}
catch (IOException ioe)
{
throw new MojoExecutionException( ioe.getMessage(), ioe );
}
ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, retroPomFile );
artifact.addMetadata( metadata );
}
public ArtifactDeployer getDeployer()
{
return deployer;
}
public void setDeployer(ArtifactDeployer deployer)
{
this.deployer = deployer;
}
public ArtifactRepository getLocalRepository()
{
return localRepository;
}
public void setLocalRepository(ArtifactRepository localRepository)
{
this.localRepository = localRepository;
}
public String getOutputPath()
{
return this.outputDirectory;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy