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

Commit b2502e2

Browse files
committed
LinkMojo creates symbolic links to artifacts
Warning: Windows requires users to have right to create symlinks, which by default only admins have in elevated mode. Hence TestLinkMojo and LinkMojo both will fail if not executed with sufficient access rights and / or elevated mode.
1 parent 4bce9ef commit b2502e2

File tree

6 files changed

+1073
-1
lines changed

6 files changed

+1073
-1
lines changed

maven-dependency-plugin/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ under the License.
178178
<dependency>
179179
<groupId>org.codehaus.plexus</groupId>
180180
<artifactId>plexus-utils</artifactId>
181-
<version>3.0.24</version>
181+
<version>3.0.25-SNAPSHOT</version>
182182
</dependency>
183183
<dependency>
184184
<groupId>org.apache.maven.shared</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
package org.apache.maven.plugins.dependency.fromConfiguration;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import org.apache.maven.plugin.MojoExecutionException;
23+
import org.apache.maven.plugin.MojoFailureException;
24+
import org.apache.maven.plugins.dependency.utils.filters.ArtifactItemFilter;
25+
import org.apache.maven.plugins.dependency.utils.filters.DestFileFilter;
26+
import org.apache.maven.plugins.annotations.LifecyclePhase;
27+
import org.apache.maven.plugins.annotations.Mojo;
28+
import org.apache.maven.plugins.annotations.Parameter;
29+
30+
import java.io.File;
31+
import java.util.List;
32+
33+
/**
34+
* Goal that creates links to a list of artifacts in the repository.
35+
*
36+
* @author <a href="mailto:[email protected]">Markus KARG</a>
37+
* @version $Id$
38+
* @since 3.0
39+
*/
40+
@Mojo( name = "link", defaultPhase = LifecyclePhase.PROCESS_SOURCES, requiresProject = false, threadSafe = true )
41+
public class LinkMojo
42+
extends AbstractFromConfigurationMojo
43+
{
44+
45+
/**
46+
* Strip artifact version during link
47+
*/
48+
@Parameter( property = "mdep.stripVersion", defaultValue = "false" )
49+
private boolean stripVersion = false;
50+
51+
/**
52+
* Strip artifact classifier during link
53+
*/
54+
@Parameter( property = "mdep.stripClassifier", defaultValue = "false" )
55+
private boolean stripClassifier = false;
56+
57+
/**
58+
* Prepend artifact groupId during link
59+
* @since 2.7
60+
*/
61+
@Parameter( property = "mdep.prependGroupId", defaultValue = "false" )
62+
private boolean prependGroupId = false;
63+
64+
/**
65+
* Use artifact baseVersion during link
66+
* @since 2.7
67+
*/
68+
@Parameter( property = "mdep.useBaseVersion", defaultValue = "false" )
69+
private boolean useBaseVersion = false;
70+
71+
/**
72+
* The artifact to link to from commandLine.
73+
* Use {@link #artifactItems} within the pom-configuration.
74+
*/
75+
@SuppressWarnings( "unused" ) //marker-field, setArtifact(String) does the magic
76+
@Parameter( property = "artifact" )
77+
private String artifact;
78+
79+
/**
80+
* <i>not used in this goal</i>
81+
*/
82+
@Parameter
83+
protected boolean useJvmChmod = true;
84+
85+
/**
86+
* <i>not used in this goal</i>
87+
*/
88+
@Parameter
89+
protected boolean ignorePermissions;
90+
91+
/**
92+
* Main entry into mojo. This method gets the ArtifactItems and iterates through each one passing it to
93+
* linkArtifact.
94+
*
95+
* @throws MojoExecutionException with a message if an error occurs.
96+
* @see ArtifactItem
97+
* @see #getArtifactItems
98+
* @see #linkArtifact(ArtifactItem)
99+
*/
100+
@Override
101+
protected void doExecute()
102+
throws MojoExecutionException, MojoFailureException
103+
{
104+
verifyRequirements();
105+
106+
List<ArtifactItem> theArtifactItems =
107+
getProcessedArtifactItems( new ProcessArtifactItemsRequest( stripVersion, prependGroupId,
108+
useBaseVersion, stripClassifier ) );
109+
for ( ArtifactItem artifactItem : theArtifactItems )
110+
{
111+
if ( artifactItem.isNeedsProcessing() )
112+
{
113+
linkArtifact( artifactItem );
114+
}
115+
else
116+
{
117+
this.getLog().info( artifactItem + " already exists in " + artifactItem.getOutputDirectory() );
118+
}
119+
}
120+
}
121+
122+
/**
123+
* Resolves the artifact from the repository and links to it from the specified location.
124+
*
125+
* @param artifactItem containing the information about the Artifact to link.
126+
* @throws MojoExecutionException with a message if an error occurs.
127+
* @see DependencyUtil#linkFile(File, File, Log)
128+
* @see DependencyUtil#getFormattedFileName(Artifact, boolean)
129+
*/
130+
protected void linkArtifact( ArtifactItem artifactItem )
131+
throws MojoExecutionException
132+
{
133+
File destFile = new File( artifactItem.getOutputDirectory(), artifactItem.getDestFileName() );
134+
135+
linkFile( artifactItem.getArtifact().getFile(), destFile );
136+
}
137+
138+
@Override
139+
protected ArtifactItemFilter getMarkedArtifactFilter( ArtifactItem item )
140+
{
141+
ArtifactItemFilter destinationNameOverrideFilter =
142+
new DestFileFilter( this.isOverWriteReleases(), this.isOverWriteSnapshots(), this.isOverWriteIfNewer(),
143+
false, false, false, false, this.stripVersion, prependGroupId, useBaseVersion,
144+
item.getOutputDirectory() );
145+
return destinationNameOverrideFilter;
146+
}
147+
148+
/**
149+
* @return Returns the stripVersion.
150+
*/
151+
public boolean isStripVersion()
152+
{
153+
return this.stripVersion;
154+
}
155+
156+
/**
157+
* @param stripVersion The stripVersion to set.
158+
*/
159+
public void setStripVersion( boolean stripVersion )
160+
{
161+
this.stripVersion = stripVersion;
162+
}
163+
164+
/**
165+
* @return Returns the stripClassifier.
166+
*/
167+
public boolean isStripClassifier()
168+
{
169+
return this.stripClassifier;
170+
}
171+
172+
/**
173+
* @param stripClassifier The stripClassifier to set.
174+
*/
175+
public void setStripClassifier( boolean stripClassifier )
176+
{
177+
this.stripClassifier = stripClassifier;
178+
}
179+
180+
/**
181+
* @param useBaseVersion The useBaseVersion to set.
182+
*/
183+
public void setUseBaseVersion( boolean useBaseVersion )
184+
{
185+
this.useBaseVersion = useBaseVersion;
186+
}
187+
}

maven-dependency-plugin/src/site/apt/index.apt.vm

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ ${project.name}
7070
*{{{./go-offline-mojo.html}dependency:go-offline}} tells Maven to resolve everything this project is dependent on
7171
(dependencies, plugins, reports) in preparation for going offline.
7272

73+
*{{{./link-mojo.html}dependency:link}} like copy but creates symbolic links.
74+
7375
*{{{./list-mojo.html}dependency:list}} alias for resolve that lists the dependencies for this project.
7476

7577
*{{{./list-repositories-mojo.html}dependency:list-repositories}} displays all project dependencies and then lists the repositories used.

maven-dependency-plugin/src/site/fml/faq.fml

+8
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ under the License.
7878
</p>
7979
</answer>
8080
</faq>
81+
<faq id="link-insufficient-rights">
82+
<question>Why do I get "You do not have sufficient privilege to perform this operation" when using the link goal?</question>
83+
<answer><p>
84+
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.
85+
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.
86+
</p>
87+
</answer>
88+
</faq>
8189
<faq id="unused">
8290
<question>Why does my dependency analysis report <code>Unused declared dependencies</code>?</question>
8391
<answer><p>

0 commit comments

Comments
 (0)