Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

LinkMojo creates symbolic links to artifacts #86

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion maven-dependency-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ under the License.
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.0.24</version>
<version>3.0.25-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.util.List;

import org.apache.maven.artifact.Artifact;
Expand All @@ -43,6 +44,7 @@
import org.codehaus.plexus.archiver.zip.ZipUnArchiver;
import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.NioFiles;
import org.codehaus.plexus.util.ReflectionUtils;
import org.codehaus.plexus.util.StringUtils;

Expand Down Expand Up @@ -186,6 +188,39 @@ protected void copyFile( File artifact, File destFile )
}
}

/**
* Does the actual linking of the file and logging.
*
* @param artifact represents the file to link to.
* @param link file name of link.
* @throws MojoExecutionException with a message if an
* error occurs.
*/
protected void linkFile( File artifact, File link )
throws MojoExecutionException
{
try
{
getLog().info( "Creating link " + link + " to "
+ ( this.outputAbsoluteArtifactFilename ? artifact.getAbsolutePath() : artifact.getName() ) );

if ( artifact.isDirectory() )
{
// usual case is a future jar packaging, but there are special cases: classifier and other packaging
throw new MojoExecutionException( "Artifact has not been packaged yet. When used on reactor artifact, "
+ "copy should be executed after packaging: see MDEP-187." );
}

if ( link.getParentFile() != null )
Files.createDirectories( link.toPath().getParent() );
NioFiles.createSymbolicLink( link, artifact);
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error creating link " + link + " to " + artifact, e );
}
}

protected void unpack( Artifact artifact, File location, String encoding )
throws MojoExecutionException
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package org.apache.maven.plugins.dependency.fromConfiguration;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.dependency.utils.filters.ArtifactItemFilter;
import org.apache.maven.plugins.dependency.utils.filters.DestFileFilter;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.util.List;

/**
* Goal that creates links to a list of artifacts in the repository.
*
* @author <a href="mailto:[email protected]">Markus KARG</a>
* @version $Id$
* @since 3.0
*/
@Mojo( name = "link", defaultPhase = LifecyclePhase.PROCESS_SOURCES, requiresProject = false, threadSafe = true )
public class LinkMojo
extends AbstractFromConfigurationMojo
{

/**
* Strip artifact version during link
*/
@Parameter( property = "mdep.stripVersion", defaultValue = "false" )
private boolean stripVersion = false;

/**
* Strip artifact classifier during link
*/
@Parameter( property = "mdep.stripClassifier", defaultValue = "false" )
private boolean stripClassifier = false;

/**
* Prepend artifact groupId during link
* @since 2.7
*/
@Parameter( property = "mdep.prependGroupId", defaultValue = "false" )
private boolean prependGroupId = false;

/**
* Use artifact baseVersion during link
* @since 2.7
*/
@Parameter( property = "mdep.useBaseVersion", defaultValue = "false" )
private boolean useBaseVersion = false;

/**
* The artifact to link to from commandLine.
* Use {@link #artifactItems} within the pom-configuration.
*/
@SuppressWarnings( "unused" ) //marker-field, setArtifact(String) does the magic
@Parameter( property = "artifact" )
private String artifact;

/**
* <i>not used in this goal</i>
*/
@Parameter
protected boolean useJvmChmod = true;

/**
* <i>not used in this goal</i>
*/
@Parameter
protected boolean ignorePermissions;

/**
* Main entry into mojo. This method gets the ArtifactItems and iterates through each one passing it to
* linkArtifact.
*
* @throws MojoExecutionException with a message if an error occurs.
* @see ArtifactItem
* @see #getArtifactItems
* @see #linkArtifact(ArtifactItem)
*/
@Override
protected void doExecute()
throws MojoExecutionException, MojoFailureException
{
verifyRequirements();

List<ArtifactItem> theArtifactItems =
getProcessedArtifactItems( new ProcessArtifactItemsRequest( stripVersion, prependGroupId,
useBaseVersion, stripClassifier ) );
for ( ArtifactItem artifactItem : theArtifactItems )
{
if ( artifactItem.isNeedsProcessing() )
{
linkArtifact( artifactItem );
}
else
{
this.getLog().info( artifactItem + " already exists in " + artifactItem.getOutputDirectory() );
}
}
}

/**
* Resolves the artifact from the repository and links to it from the specified location.
*
* @param artifactItem containing the information about the Artifact to link.
* @throws MojoExecutionException with a message if an error occurs.
* @see DependencyUtil#linkFile(File, File, Log)
* @see DependencyUtil#getFormattedFileName(Artifact, boolean)
*/
protected void linkArtifact( ArtifactItem artifactItem )
throws MojoExecutionException
{
File destFile = new File( artifactItem.getOutputDirectory(), artifactItem.getDestFileName() );

linkFile( artifactItem.getArtifact().getFile(), destFile );
}

@Override
protected ArtifactItemFilter getMarkedArtifactFilter( ArtifactItem item )
{
ArtifactItemFilter destinationNameOverrideFilter =
new DestFileFilter( this.isOverWriteReleases(), this.isOverWriteSnapshots(), this.isOverWriteIfNewer(),
false, false, false, false, this.stripVersion, prependGroupId, useBaseVersion,
item.getOutputDirectory() );
return destinationNameOverrideFilter;
}

/**
* @return Returns the stripVersion.
*/
public boolean isStripVersion()
{
return this.stripVersion;
}

/**
* @param stripVersion The stripVersion to set.
*/
public void setStripVersion( boolean stripVersion )
{
this.stripVersion = stripVersion;
}

/**
* @return Returns the stripClassifier.
*/
public boolean isStripClassifier()
{
return this.stripClassifier;
}

/**
* @param stripClassifier The stripClassifier to set.
*/
public void setStripClassifier( boolean stripClassifier )
{
this.stripClassifier = stripClassifier;
}

/**
* @param useBaseVersion The useBaseVersion to set.
*/
public void setUseBaseVersion( boolean useBaseVersion )
{
this.useBaseVersion = useBaseVersion;
}
}
2 changes: 2 additions & 0 deletions maven-dependency-plugin/src/site/apt/index.apt.vm
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ ${project.name}
*{{{./go-offline-mojo.html}dependency:go-offline}} tells Maven to resolve everything this project is dependent on
(dependencies, plugins, reports) in preparation for going offline.

*{{{./link-mojo.html}dependency:link}} like copy but creates symbolic links.

*{{{./list-mojo.html}dependency:list}} alias for resolve that lists the dependencies for this project.

*{{{./list-repositories-mojo.html}dependency:list-repositories}} displays all project dependencies and then lists the repositories used.
Expand Down
8 changes: 8 additions & 0 deletions maven-dependency-plugin/src/site/fml/faq.fml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ under the License.
</p>
</answer>
</faq>
<faq id="link-insufficient-rights">
<question>Why do I get "You do not have sufficient privilege to perform this operation" when using the link goal?</question>
<answer><p>
This is a security constraint of the Windows(R) operating system. By default, it prevents anybody to create symbolic links besides administrators in elevated mode.
Either execute Maven as an administrator in elevated mode, or as a plain user having explicitly granted the right "Create symbolic links" by an administrator.
</p>
</answer>
</faq>
<faq id="unused">
<question>Why does my dependency analysis report <code>Unused declared dependencies</code>?</question>
<answer><p>
Expand Down
Loading