Skip to content

Commit 569b31a

Browse files
[MINSTALL-183] Don't use metadata from main artifact to fetch pom.xml
1 parent c172567 commit 569b31a

File tree

2 files changed

+24
-62
lines changed

2 files changed

+24
-62
lines changed

src/main/java/org/apache/maven/plugins/install/InstallMojo.java

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,21 @@
3030
import org.apache.maven.model.PluginExecution;
3131
import org.apache.maven.plugin.AbstractMojo;
3232
import org.apache.maven.plugin.MojoExecutionException;
33-
import org.apache.maven.plugin.MojoFailureException;
3433
import org.apache.maven.plugin.descriptor.PluginDescriptor;
3534
import org.apache.maven.plugins.annotations.Component;
3635
import org.apache.maven.plugins.annotations.LifecyclePhase;
3736
import org.apache.maven.plugins.annotations.Mojo;
3837
import org.apache.maven.plugins.annotations.Parameter;
3938
import org.apache.maven.project.MavenProject;
4039
import org.apache.maven.project.artifact.ProjectArtifact;
41-
import org.apache.maven.project.artifact.ProjectArtifactMetadata;
4240
import org.eclipse.aether.RepositorySystem;
43-
import org.eclipse.aether.artifact.Artifact;
4441
import org.eclipse.aether.installation.InstallRequest;
4542
import org.eclipse.aether.installation.InstallationException;
46-
import org.eclipse.aether.util.artifact.SubArtifact;
4743

4844
/**
4945
* Installs the project's main artifact, and any other artifacts attached by other plugins in the lifecycle, to the
5046
* local repository.
51-
*
47+
*
5248
* @author <a href="mailto:[email protected]">Emmanuel Venisse</a>
5349
*/
5450
@Mojo( name = "install", defaultPhase = LifecyclePhase.INSTALL, threadSafe = true )
@@ -74,7 +70,7 @@ public class InstallMojo
7470
* Whether every project should be installed during its own install-phase or at the end of the multimodule build. If
7571
* set to {@code true} and the build fails, none of the reactor projects is installed.
7672
* <strong>(experimental)</strong>
77-
*
73+
*
7874
* @since 2.5
7975
*/
8076
@Parameter( defaultValue = "false", property = "installAtEnd" )
@@ -83,7 +79,7 @@ public class InstallMojo
8379
/**
8480
* Set this to <code>true</code> to bypass artifact installation. Use this for artifacts that do not need to be
8581
* installed in the local repository.
86-
*
82+
*
8783
* @since 2.4
8884
*/
8985
@Parameter( property = "maven.install.skip", defaultValue = "false" )
@@ -115,7 +111,7 @@ private boolean hasState( MavenProject project )
115111

116112
@Override
117113
public void execute()
118-
throws MojoExecutionException, MojoFailureException
114+
throws MojoExecutionException
119115
{
120116
if ( skip )
121117
{
@@ -194,16 +190,12 @@ private boolean hasExecution( Plugin plugin )
194190
return false;
195191
}
196192

197-
private void installProject( MavenProject project ) throws MojoExecutionException, MojoFailureException
193+
private void installProject( MavenProject project ) throws MojoExecutionException
198194
{
199195
try
200196
{
201197
repositorySystem.install( session.getRepositorySession(), processProject( project ) );
202198
}
203-
catch ( IllegalArgumentException e )
204-
{
205-
throw new MojoFailureException( e.getMessage(), e );
206-
}
207199
catch ( InstallationException e )
208200
{
209201
throw new MojoExecutionException( e.getMessage(), e );
@@ -213,63 +205,40 @@ private void installProject( MavenProject project ) throws MojoExecutionExceptio
213205
/**
214206
* Processes passed in {@link MavenProject} and produces {@link InstallRequest} out of it.
215207
*
216-
* @throws IllegalArgumentException if project is badly set up.
208+
* @throws MojoExecutionException if project is badly set up.
217209
*/
218-
private InstallRequest processProject( MavenProject project )
210+
private InstallRequest processProject( MavenProject project ) throws MojoExecutionException
219211
{
220212
InstallRequest request = new InstallRequest();
221-
org.apache.maven.artifact.Artifact mavenMainArtifact = project.getArtifact();
222-
String packaging = project.getPackaging();
223-
File pomFile = project.getFile();
224-
boolean isPomArtifact = "pom".equals( packaging );
225-
boolean pomArtifactAttached = false;
226213

227-
if ( pomFile != null )
214+
if ( isFile( project.getFile() ) )
228215
{
229216
request.addArtifact( RepositoryUtils.toArtifact( new ProjectArtifact( project ) ) );
230-
pomArtifactAttached = true;
217+
}
218+
else
219+
{
220+
throw new MojoExecutionException( "The project POM could not be attached" );
231221
}
232222

233-
if ( !isPomArtifact )
223+
if ( !"pom".equals( project.getPackaging() ) )
234224
{
235-
File file = mavenMainArtifact.getFile();
236-
if ( file != null && file.isFile() )
225+
org.apache.maven.artifact.Artifact mavenMainArtifact = project.getArtifact();
226+
if ( isFile( mavenMainArtifact.getFile() ) )
237227
{
238-
Artifact mainArtifact = RepositoryUtils.toArtifact( mavenMainArtifact );
239-
request.addArtifact( mainArtifact );
240-
241-
if ( !pomArtifactAttached )
242-
{
243-
for ( Object metadata : mavenMainArtifact.getMetadataList() )
244-
{
245-
if ( metadata instanceof ProjectArtifactMetadata )
246-
{
247-
request.addArtifact( new SubArtifact(
248-
mainArtifact,
249-
"",
250-
"pom"
251-
).setFile( ( (ProjectArtifactMetadata) metadata ).getFile() ) );
252-
pomArtifactAttached = true;
253-
}
254-
}
255-
}
228+
request.addArtifact( RepositoryUtils.toArtifact( mavenMainArtifact ) );
256229
}
257230
else if ( !project.getAttachedArtifacts().isEmpty() )
258231
{
259-
throw new IllegalArgumentException( "The packaging plugin for this project did not assign "
232+
throw new MojoExecutionException( "The packaging plugin for this project did not assign "
260233
+ "a main file to the project but it has attachments. Change packaging to 'pom'." );
261234
}
262235
else
263236
{
264-
throw new IllegalArgumentException( "The packaging for this project did not assign "
237+
throw new MojoExecutionException( "The packaging for this project did not assign "
265238
+ "a file to the build artifact" );
266239
}
267240
}
268241

269-
if ( !pomArtifactAttached )
270-
{
271-
throw new IllegalArgumentException( "The POM could not be attached" );
272-
}
273242

274243
for ( org.apache.maven.artifact.Artifact attached : project.getAttachedArtifacts() )
275244
{
@@ -280,4 +249,8 @@ else if ( !project.getAttachedArtifacts().isEmpty() )
280249
return request;
281250
}
282251

252+
private boolean isFile( File file )
253+
{
254+
return file != null && file.isFile();
255+
}
283256
}

src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@
2525
import java.util.concurrent.ConcurrentHashMap;
2626

2727
import org.apache.maven.artifact.Artifact;
28-
import org.apache.maven.artifact.metadata.ArtifactMetadata;
2928
import org.apache.maven.execution.MavenSession;
3029
import org.apache.maven.model.Build;
3130
import org.apache.maven.model.Plugin;
3231
import org.apache.maven.plugin.AbstractMojo;
33-
import org.apache.maven.plugin.MojoFailureException;
32+
import org.apache.maven.plugin.MojoExecutionException;
3433
import org.apache.maven.plugin.descriptor.PluginDescriptor;
3534
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
3635
import org.apache.maven.plugins.install.stubs.AttachedArtifactStub0;
@@ -216,7 +215,7 @@ public void testInstallIfArtifactFileIsNull()
216215

217216
fail( "Did not throw mojo execution exception" );
218217
}
219-
catch ( MojoFailureException e )
218+
catch ( MojoExecutionException e )
220219
{
221220
//expected
222221
}
@@ -286,16 +285,6 @@ public void testBasicInstallAndCreate()
286285

287286
mojo.execute();
288287

289-
ArtifactMetadata metadata = null;
290-
for ( Object o : artifact.getMetadataList() )
291-
{
292-
metadata = (ArtifactMetadata) o;
293-
if ( metadata.getRemoteFilename().endsWith( "pom" ) )
294-
{
295-
break;
296-
}
297-
}
298-
299288
File pom = new File( new File( LOCAL_REPO ), mavenSession.getRepositorySession().getLocalRepositoryManager().getPathForLocalArtifact( new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), "pom", artifact.getVersion() ) ) );
300289

301290
assertTrue( pom.exists() );

0 commit comments

Comments
 (0)