Skip to content

Commit

Permalink
[MINSTALL-183] Don't use metadata from main artifact to fetch pom.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Nov 6, 2022
1 parent c172567 commit 569b31a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 62 deletions.
71 changes: 22 additions & 49 deletions src/main/java/org/apache/maven/plugins/install/InstallMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,21 @@
import org.apache.maven.model.PluginExecution;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.artifact.ProjectArtifact;
import org.apache.maven.project.artifact.ProjectArtifactMetadata;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.installation.InstallRequest;
import org.eclipse.aether.installation.InstallationException;
import org.eclipse.aether.util.artifact.SubArtifact;

/**
* Installs the project's main artifact, and any other artifacts attached by other plugins in the lifecycle, to the
* local repository.
*
*
* @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
*/
@Mojo( name = "install", defaultPhase = LifecyclePhase.INSTALL, threadSafe = true )
Expand All @@ -74,7 +70,7 @@ public class InstallMojo
* Whether every project should be installed during its own install-phase or at the end of the multimodule build. If
* set to {@code true} and the build fails, none of the reactor projects is installed.
* <strong>(experimental)</strong>
*
*
* @since 2.5
*/
@Parameter( defaultValue = "false", property = "installAtEnd" )
Expand All @@ -83,7 +79,7 @@ public class InstallMojo
/**
* Set this to <code>true</code> to bypass artifact installation. Use this for artifacts that do not need to be
* installed in the local repository.
*
*
* @since 2.4
*/
@Parameter( property = "maven.install.skip", defaultValue = "false" )
Expand Down Expand Up @@ -115,7 +111,7 @@ private boolean hasState( MavenProject project )

@Override
public void execute()
throws MojoExecutionException, MojoFailureException
throws MojoExecutionException
{
if ( skip )
{
Expand Down Expand Up @@ -194,16 +190,12 @@ private boolean hasExecution( Plugin plugin )
return false;
}

private void installProject( MavenProject project ) throws MojoExecutionException, MojoFailureException
private void installProject( MavenProject project ) throws MojoExecutionException
{
try
{
repositorySystem.install( session.getRepositorySession(), processProject( project ) );
}
catch ( IllegalArgumentException e )
{
throw new MojoFailureException( e.getMessage(), e );
}
catch ( InstallationException e )
{
throw new MojoExecutionException( e.getMessage(), e );
Expand All @@ -213,63 +205,40 @@ private void installProject( MavenProject project ) throws MojoExecutionExceptio
/**
* Processes passed in {@link MavenProject} and produces {@link InstallRequest} out of it.
*
* @throws IllegalArgumentException if project is badly set up.
* @throws MojoExecutionException if project is badly set up.
*/
private InstallRequest processProject( MavenProject project )
private InstallRequest processProject( MavenProject project ) throws MojoExecutionException
{
InstallRequest request = new InstallRequest();
org.apache.maven.artifact.Artifact mavenMainArtifact = project.getArtifact();
String packaging = project.getPackaging();
File pomFile = project.getFile();
boolean isPomArtifact = "pom".equals( packaging );
boolean pomArtifactAttached = false;

if ( pomFile != null )
if ( isFile( project.getFile() ) )
{
request.addArtifact( RepositoryUtils.toArtifact( new ProjectArtifact( project ) ) );
pomArtifactAttached = true;
}
else
{
throw new MojoExecutionException( "The project POM could not be attached" );
}

if ( !isPomArtifact )
if ( !"pom".equals( project.getPackaging() ) )
{
File file = mavenMainArtifact.getFile();
if ( file != null && file.isFile() )
org.apache.maven.artifact.Artifact mavenMainArtifact = project.getArtifact();
if ( isFile( mavenMainArtifact.getFile() ) )
{
Artifact mainArtifact = RepositoryUtils.toArtifact( mavenMainArtifact );
request.addArtifact( mainArtifact );

if ( !pomArtifactAttached )
{
for ( Object metadata : mavenMainArtifact.getMetadataList() )
{
if ( metadata instanceof ProjectArtifactMetadata )
{
request.addArtifact( new SubArtifact(
mainArtifact,
"",
"pom"
).setFile( ( (ProjectArtifactMetadata) metadata ).getFile() ) );
pomArtifactAttached = true;
}
}
}
request.addArtifact( RepositoryUtils.toArtifact( mavenMainArtifact ) );
}
else if ( !project.getAttachedArtifacts().isEmpty() )
{
throw new IllegalArgumentException( "The packaging plugin for this project did not assign "
throw new MojoExecutionException( "The packaging plugin for this project did not assign "
+ "a main file to the project but it has attachments. Change packaging to 'pom'." );
}
else
{
throw new IllegalArgumentException( "The packaging for this project did not assign "
throw new MojoExecutionException( "The packaging for this project did not assign "
+ "a file to the build artifact" );
}
}

if ( !pomArtifactAttached )
{
throw new IllegalArgumentException( "The POM could not be attached" );
}

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

private boolean isFile( File file )
{
return file != null && file.isFile();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@
import java.util.concurrent.ConcurrentHashMap;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ArtifactMetadata;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Build;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugins.install.stubs.AttachedArtifactStub0;
Expand Down Expand Up @@ -216,7 +215,7 @@ public void testInstallIfArtifactFileIsNull()

fail( "Did not throw mojo execution exception" );
}
catch ( MojoFailureException e )
catch ( MojoExecutionException e )
{
//expected
}
Expand Down Expand Up @@ -286,16 +285,6 @@ public void testBasicInstallAndCreate()

mojo.execute();

ArtifactMetadata metadata = null;
for ( Object o : artifact.getMetadataList() )
{
metadata = (ArtifactMetadata) o;
if ( metadata.getRemoteFilename().endsWith( "pom" ) )
{
break;
}
}

File pom = new File( new File( LOCAL_REPO ), mavenSession.getRepositorySession().getLocalRepositoryManager().getPathForLocalArtifact( new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), "pom", artifact.getVersion() ) ) );

assertTrue( pom.exists() );
Expand Down

0 comments on commit 569b31a

Please sign in to comment.