From c5076945cd61b3770a0f2ca89e55f389d7cdfac7 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Mon, 20 Jun 2022 14:45:35 +0200 Subject: [PATCH 01/10] Drop MAT Now that Maven is 3.2.5, no need for MAT anymore. --- pom.xml | 35 +++--- .../plugins/install/AbstractInstallMojo.java | 101 +++++++++++++++--- .../plugins/install/InstallFileMojo.java | 67 ++++++------ .../maven/plugins/install/InstallMojo.java | 50 ++------- .../plugins/install/InstallFileMojoTest.java | 9 +- .../plugins/install/InstallMojoTest.java | 13 +-- 6 files changed, 157 insertions(+), 118 deletions(-) diff --git a/pom.xml b/pom.xml index 74307477..627f549f 100644 --- a/pom.xml +++ b/pom.xml @@ -63,10 +63,10 @@ - 3.2.5 - 1.7.32 - 1.1.0 7 + 3.2.5 + 1.0.0.v20140518 + 1.7.5 2020-04-07T21:04:00Z @@ -97,9 +97,20 @@ provided - org.apache.maven.shared - maven-artifact-transfer - 0.13.1 + org.eclipse.aether + aether-api + ${aetherVersion} + compile + + + org.eclipse.aether + aether-util + ${aetherVersion} + compile + + + org.codehaus.plexus + plexus-utils @@ -145,18 +156,6 @@ ${slf4jVersion} test - - org.eclipse.aether - aether-api - ${aetherVersion} - test - - - org.eclipse.aether - aether-util - ${aetherVersion} - test - org.eclipse.aether aether-impl diff --git a/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java b/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java index 96d0f539..7143461b 100644 --- a/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java @@ -21,14 +21,22 @@ import java.io.File; +import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.artifact.ProjectArtifact; import org.apache.maven.project.artifact.ProjectArtifactMetadata; -import org.apache.maven.shared.transfer.repository.RepositoryManager; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.artifact.DefaultArtifact; +import org.eclipse.aether.installation.InstallRequest; +import org.eclipse.aether.installation.InstallationException; +import org.eclipse.aether.util.artifact.SubArtifact; /** * Common fields for installation mojos. @@ -38,9 +46,8 @@ public abstract class AbstractInstallMojo extends AbstractMojo { - @Component - protected RepositoryManager repositoryManager; + protected RepositorySystem repositorySystem; @Parameter( defaultValue = "${session}", required = true, readonly = true ) protected MavenSession session; @@ -49,28 +56,98 @@ public abstract class AbstractInstallMojo * Gets the path of the specified artifact within the local repository. Note that the returned path need not exist * (yet). * - * @param buildingRequest {@link ProjectBuildingRequest}. * @param artifact The artifact whose local repo path should be determined, must not be null. * @return The absolute path to the artifact when installed, never null. */ - protected File getLocalRepoFile( ProjectBuildingRequest buildingRequest, Artifact artifact ) + protected File getLocalRepoFile( Artifact artifact ) { - String path = repositoryManager.getPathForLocalArtifact( buildingRequest, artifact ); - return new File( repositoryManager.getLocalRepositoryBasedir( buildingRequest ), path ); + String path = session.getRepositorySession().getLocalRepositoryManager() + .getPathForLocalArtifact( RepositoryUtils.toArtifact( artifact ) ); + return new File( session.getRepositorySession().getLocalRepository().getBasedir(), path ); } /** * Gets the path of the specified artifact metadata within the local repository. Note that the returned path need * not exist (yet). * - * @param buildingRequest {@link ProjectBuildingRequest}. * @param metadata The artifact metadata whose local repo path should be determined, must not be null. * @return The absolute path to the artifact metadata when installed, never null. */ - protected File getLocalRepoFile( ProjectBuildingRequest buildingRequest, ProjectArtifactMetadata metadata ) + protected File getLocalRepoFile( ProjectArtifactMetadata metadata ) { - String path = repositoryManager.getPathForLocalMetadata( buildingRequest, metadata ); - return new File( repositoryManager.getLocalRepositoryBasedir( buildingRequest ), path ); + DefaultArtifact pomArtifact = new DefaultArtifact( + metadata.getGroupId(), + metadata.getArtifactId(), + "", + "pom", + metadata.getBaseVersion() ); + + String path = session.getRepositorySession().getLocalRepositoryManager().getPathForLocalArtifact( + pomArtifact ); + return new File( session.getRepositorySession().getLocalRepository().getBasedir(), path ); } + protected void installProject( MavenProject project ) + throws MojoFailureException, MojoExecutionException + { + try + { + InstallRequest request = new InstallRequest(); + Artifact artifact = project.getArtifact(); + String packaging = project.getPackaging(); + File pomFile = project.getFile(); + boolean isPomArtifact = "pom".equals( packaging ); + + if ( pomFile != null ) + { + request.addArtifact( RepositoryUtils.toArtifact( new ProjectArtifact( project ) ) ); + } + + if ( !isPomArtifact ) + { + File file = artifact.getFile(); + + // Here, we have a temporary solution to MINSTALL-3 (isDirectory() is true if it went through compile + // but not package). We are designing in a proper solution for Maven 2.1 + if ( file != null && file.isFile() ) + { + org.eclipse.aether.artifact.Artifact mainArtifact = RepositoryUtils.toArtifact( artifact ); + request.addArtifact( mainArtifact ); + + for ( Object metadata : artifact.getMetadataList() ) + { + if ( metadata instanceof ProjectArtifactMetadata ) + { + org.eclipse.aether.artifact.Artifact pomArtifact = + new SubArtifact( mainArtifact, "", "pom" ); + pomArtifact = pomArtifact.setFile( ( (ProjectArtifactMetadata) metadata ).getFile() ); + request.addArtifact( pomArtifact ); + } + } + } + else if ( !project.getAttachedArtifacts().isEmpty() ) + { + 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 MojoExecutionException( "The packaging for this project did not assign " + + "a file to the build artifact" ); + } + } + + for ( Artifact attached : project.getAttachedArtifacts() ) + { + getLog().debug( "Attaching for install: " + attached.getId() ); + request.addArtifact( RepositoryUtils.toArtifact( attached ) ); + } + + repositorySystem.install( session.getRepositorySession(), request ); + } + catch ( InstallationException e ) + { + throw new MojoExecutionException( e.getMessage(), e ); + } + } } diff --git a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java index 32865b29..4ec41364 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java @@ -21,12 +21,12 @@ import java.io.File; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.Writer; +import java.nio.file.Files; import java.util.Enumeration; import java.util.jar.JarEntry; import java.util.jar.JarFile; @@ -34,6 +34,7 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.handler.DefaultArtifactHandler; +import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Model; import org.apache.maven.model.Parent; import org.apache.maven.model.building.ModelBuildingException; @@ -53,14 +54,15 @@ import org.apache.maven.project.ProjectBuildingException; import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.project.artifact.ProjectArtifactMetadata; -import org.apache.maven.shared.transfer.project.install.ProjectInstaller; -import org.apache.maven.shared.transfer.project.install.ProjectInstallerRequest; -import org.apache.maven.shared.utils.Os; -import org.apache.maven.shared.utils.ReaderFactory; -import org.apache.maven.shared.utils.WriterFactory; -import org.apache.maven.shared.utils.io.IOUtil; import org.codehaus.plexus.util.FileUtils; +import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.xml.XmlStreamReader; +import org.codehaus.plexus.util.xml.XmlStreamWriter; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; +import org.eclipse.aether.DefaultRepositoryCache; +import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.repository.LocalRepository; +import org.eclipse.aether.repository.LocalRepositoryManager; /** * Installs a file in the local repository. @@ -71,6 +73,7 @@ public class InstallFileMojo extends AbstractInstallMojo { + private static final String LINE_SEP = System.getProperty( "line.separator" ); /** * GroupId of the artifact to be installed. Retrieved from POM file if one is specified or extracted from @@ -170,19 +173,12 @@ public class InstallFileMojo @Component private ProjectBuilder projectBuilder; - /** - * Used to install the project created. - */ - @Component - private ProjectInstaller installer; - /** * @see org.apache.maven.plugin.Mojo#execute() */ public void execute() throws MojoExecutionException, MojoFailureException { - if ( !file.exists() ) { String message = "The specified file '" + file.getPath() + "' does not exist"; @@ -190,16 +186,24 @@ public void execute() throw new MojoFailureException( message ); } - ProjectBuildingRequest buildingRequest = session.getProjectBuildingRequest(); - - // ---------------------------------------------------------------------- - // Override the default localRepository variable - // ---------------------------------------------------------------------- if ( localRepositoryPath != null ) { - buildingRequest = repositoryManager.setLocalRepositoryBasedir( buildingRequest, localRepositoryPath ); - - getLog().debug( "localRepoPath: " + repositoryManager.getLocalRepositoryBasedir( buildingRequest ) ); + // "clone" session and replace localRepository + DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession( + session.getRepositorySession() ); + // Clear cache, since we're using a new local repository + newSession.setCache( new DefaultRepositoryCache() ); + // keep same repositoryType + String contentType = newSession.getLocalRepository().getContentType(); + if ( "enhanced".equals( contentType ) ) + { + contentType = "default"; + } + LocalRepositoryManager localRepositoryManager = repositorySystem.newLocalRepositoryManager( newSession, + new LocalRepository( localRepositoryPath, contentType ) ); + newSession.setLocalRepositoryManager( localRepositoryManager ); + this.session = new MavenSession( + session.getContainer(), newSession, session.getRequest(), session.getResult() ); } File temporaryPom = null; @@ -225,7 +229,7 @@ public void execute() project.getArtifact().setArtifactHandler( ah ); Artifact artifact = project.getArtifact(); - if ( file.equals( getLocalRepoFile( buildingRequest, artifact ) ) ) + if ( file.equals( getLocalRepoFile( artifact ) ) ) { throw new MojoFailureException( "Cannot install artifact. " + "Artifact is already in the local repository.\n\nFile in question is: " + file + "\n" ); @@ -262,7 +266,7 @@ public void execute() temporaryPom = generatePomFile(); ProjectArtifactMetadata pomMetadata = new ProjectArtifactMetadata( artifact, temporaryPom ); if ( Boolean.TRUE.equals( generatePom ) - || ( generatePom == null && !getLocalRepoFile( buildingRequest, pomMetadata ).exists() ) ) + || ( generatePom == null && !getLocalRepoFile( pomMetadata ).exists() ) ) { getLog().debug( "Installing generated POM" ); if ( classifier == null ) @@ -293,12 +297,7 @@ else if ( generatePom == null ) try { - // CHECKSTYLE_OFF: LineLength - ProjectInstallerRequest projectInstallerRequest = - new ProjectInstallerRequest().setProject( project ); - // CHECKSTYLE_ON: LineLength - - installer.install( buildingRequest, projectInstallerRequest ); + installProject( project ); } catch ( Exception e ) { @@ -344,7 +343,7 @@ private MavenProject createMavenProject() { if ( e.getCause() instanceof ModelBuildingException ) { - throw new MojoExecutionException( "The artifact information is not valid:" + Os.LINE_SEP + throw new MojoExecutionException( "The artifact information is not valid:" + LINE_SEP + e.getCause().getMessage() ); } throw new MojoFailureException( "Unable to create the project.", e ); @@ -387,7 +386,7 @@ private File readingPomFromJarFile() } pomFile = File.createTempFile( base, ".pom" ); - pomOutputStream = new FileOutputStream( pomFile ); + pomOutputStream = Files.newOutputStream( pomFile.toPath() ); IOUtil.copy( pomInputStream, pomOutputStream ); @@ -448,7 +447,7 @@ private Model readModel( File pomFile ) Reader reader = null; try { - reader = ReaderFactory.newXmlReader( pomFile ); + reader = new XmlStreamReader( pomFile ); final Model model = new MavenXpp3Reader().read( reader ); reader.close(); reader = null; @@ -545,7 +544,7 @@ private File generatePomFile() { File pomFile = File.createTempFile( "mvninstall", ".pom" ); - writer = WriterFactory.newXmlWriter( pomFile ); + writer = new XmlStreamWriter( pomFile ); new MavenXpp3Writer().write( writer, model ); writer.close(); writer = null; diff --git a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java index b01c1141..3e3463b4 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java @@ -19,7 +19,6 @@ * under the License. */ -import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -27,16 +26,10 @@ import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; -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.ProjectBuildingRequest; -import org.apache.maven.shared.transfer.artifact.install.ArtifactInstallerException; -import org.apache.maven.shared.transfer.project.NoFileAssignedException; -import org.apache.maven.shared.transfer.project.install.ProjectInstaller; -import org.apache.maven.shared.transfer.project.install.ProjectInstallerRequest; /** * Installs the project's main artifact, and any other artifacts attached by other plugins in the lifecycle, to the @@ -55,8 +48,8 @@ public class InstallMojo */ private static final AtomicInteger READYPROJECTSCOUNTER = new AtomicInteger(); - private static final List INSTALLREQUESTS = - Collections.synchronizedList( new ArrayList() ); + private static final List INSTALLREQUESTS = + Collections.synchronizedList( new ArrayList() ); /** */ @@ -85,9 +78,6 @@ public class InstallMojo @Parameter( property = "maven.install.skip", defaultValue = "false" ) private boolean skip; - @Component - private ProjectInstaller installer; - public void execute() throws MojoExecutionException, MojoFailureException { @@ -98,18 +88,13 @@ public void execute() } else { - // CHECKSTYLE_OFF: LineLength - ProjectInstallerRequest projectInstallerRequest = - new ProjectInstallerRequest().setProject( project ); - // CHECKSTYLE_ON: LineLength - if ( !installAtEnd ) { - installProject( session.getProjectBuildingRequest(), projectInstallerRequest ); + installProject( project ); } else { - INSTALLREQUESTS.add( projectInstallerRequest ); + INSTALLREQUESTS.add( project ); addedInstallRequest = true; } } @@ -121,7 +106,7 @@ public void execute() { while ( !INSTALLREQUESTS.isEmpty() ) { - installProject( session.getProjectBuildingRequest(), INSTALLREQUESTS.remove( 0 ) ); + installProject( INSTALLREQUESTS.remove( 0 ) ); } } } @@ -132,28 +117,9 @@ else if ( addedInstallRequest ) } } - private void installProject( ProjectBuildingRequest pbr, ProjectInstallerRequest pir ) - throws MojoFailureException, MojoExecutionException - { - try - { - installer.install( session.getProjectBuildingRequest(), pir ); - } - catch ( IOException e ) - { - throw new MojoFailureException( "IOException", e ); - } - catch ( ArtifactInstallerException e ) - { - throw new MojoExecutionException( "ArtifactInstallerException", e ); - } - catch ( NoFileAssignedException e ) - { - throw new MojoExecutionException( "NoFileAssignedException", e ); - } - - } - + /** + * Visible for testing. + */ public void setSkip( boolean skip ) { this.skip = skip; diff --git a/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java b/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java index bf040b15..c135d959 100644 --- a/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java @@ -28,8 +28,8 @@ import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.ProjectBuildingRequest; -import org.apache.maven.shared.utils.ReaderFactory; -import org.apache.maven.shared.utils.io.FileUtils; +import org.codehaus.plexus.util.FileUtils; +import org.codehaus.plexus.util.xml.XmlStreamReader; import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory; import org.eclipse.aether.repository.LocalRepository; @@ -159,7 +159,7 @@ public void testInstallFileWithGeneratePom() File installedPom = new File( getBasedir(), LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + "." + "pom" ); - try ( Reader reader = ReaderFactory.newXmlReader( installedPom ) ) { + try ( Reader reader = new XmlStreamReader( installedPom ) ) { Model model = new MavenXpp3Reader().read( reader ); assertEquals( "4.0.0", model.getModelVersion() ); @@ -258,7 +258,7 @@ public void testInstallFile() assertTrue( installedArtifact.exists() ); - assertEquals( 5, FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).size() ); + assertEquals( FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).toString(), 5, FileUtils.getFiles( new File( LOCAL_REPO ), null, null ).size() ); } private void assignValuesForParameter( Object obj ) @@ -294,6 +294,7 @@ repositorySession, new LocalRepository( LOCAL_REPO ) ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); buildingRequest.setRepositorySession( repositorySession ); when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest ); + when( session.getRepositorySession() ).thenReturn( repositorySession ); return session; } } diff --git a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java index ed73d2cb..374872a9 100644 --- a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java @@ -26,6 +26,7 @@ import java.util.Collections; import java.util.List; +import org.apache.maven.RepositoryUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.metadata.ArtifactMetadata; import org.apache.maven.execution.MavenSession; @@ -36,9 +37,9 @@ import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.MavenProject; import org.apache.maven.project.ProjectBuildingRequest; -import org.apache.maven.shared.transfer.repository.RepositoryManager; -import org.apache.maven.shared.utils.io.FileUtils; +import org.codehaus.plexus.util.FileUtils; import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.artifact.DefaultArtifact; import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory; import org.eclipse.aether.repository.LocalRepository; import org.eclipse.aether.repository.NoLocalRepositoryManagerException; @@ -279,12 +280,7 @@ public void testBasicInstallAndCreate() } } - RepositoryManager repoManager = (RepositoryManager) getVariableValueFromObject( mojo, "repositoryManager" ); - - ProjectBuildingRequest pbr = mavenSession.getProjectBuildingRequest(); - - File pom = new File( repoManager.getLocalRepositoryBasedir( pbr ), - repoManager.getPathForLocalMetadata( pbr, metadata ) ); + File pom = new File( new File( LOCAL_REPO ), mavenSession.getRepositorySession().getLocalRepositoryManager().getPathForLocalArtifact( new DefaultArtifact( artifact.getGroupId(), artifact.getArtifactId(), "pom", artifact.getVersion() ) ) ); assertTrue( pom.exists() ); @@ -357,6 +353,7 @@ repositorySession, new LocalRepository( LOCAL_REPO ) ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); buildingRequest.setRepositorySession( repositorySession ); when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest ); + when( session.getRepositorySession() ).thenReturn( repositorySession ); return session; } From c24e909196b738b9701cd484afeb1621f9200e66 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Mon, 20 Jun 2022 14:55:24 +0200 Subject: [PATCH 02/10] Fix scopes --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 627f549f..e556d80f 100644 --- a/pom.xml +++ b/pom.xml @@ -100,13 +100,13 @@ org.eclipse.aether aether-api ${aetherVersion} - compile + provided org.eclipse.aether aether-util ${aetherVersion} - compile + compile org.codehaus.plexus From 51bc6fc5b8e607aaa75176049c1c915a50adb254 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Tue, 5 Jul 2022 11:26:37 +0200 Subject: [PATCH 03/10] Clean up use of LS --- .../org/apache/maven/plugins/install/InstallFileMojo.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java index 4ec41364..c6d527d7 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java @@ -73,7 +73,7 @@ public class InstallFileMojo extends AbstractInstallMojo { - private static final String LINE_SEP = System.getProperty( "line.separator" ); + private static final String LS = System.getProperty( "line.separator" ); /** * GroupId of the artifact to be installed. Retrieved from POM file if one is specified or extracted from @@ -188,7 +188,7 @@ public void execute() if ( localRepositoryPath != null ) { - // "clone" session and replace localRepository + // "clone" repository session and replace localRepository DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession( session.getRepositorySession() ); // Clear cache, since we're using a new local repository @@ -232,7 +232,7 @@ public void execute() if ( file.equals( getLocalRepoFile( artifact ) ) ) { throw new MojoFailureException( "Cannot install artifact. " - + "Artifact is already in the local repository.\n\nFile in question is: " + file + "\n" ); + + "Artifact is already in the local repository." + LS + LS + "File in question is: " + file + LS ); } if ( classifier == null ) @@ -343,7 +343,7 @@ private MavenProject createMavenProject() { if ( e.getCause() instanceof ModelBuildingException ) { - throw new MojoExecutionException( "The artifact information is not valid:" + LINE_SEP + throw new MojoExecutionException( "The artifact information is not valid:" + LS + e.getCause().getMessage() ); } throw new MojoFailureException( "Unable to create the project.", e ); From a32f8c26840314b069846e92ed7c7ec1a0152f25 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Tue, 5 Jul 2022 14:43:08 +0200 Subject: [PATCH 04/10] Make plugin simpler --- .../plugins/install/AbstractInstallMojo.java | 115 +------------ .../plugins/install/InstallFileMojo.java | 160 +++++------------- .../maven/plugins/install/InstallMojo.java | 18 ++ .../maven/plugins/install/Installer.java | 143 ++++++++++++++++ .../plugins/install/InstallFileMojoTest.java | 5 + .../plugins/install/InstallMojoTest.java | 3 +- 6 files changed, 217 insertions(+), 227 deletions(-) create mode 100644 src/main/java/org/apache/maven/plugins/install/Installer.java diff --git a/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java b/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java index 7143461b..28aa4f36 100644 --- a/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java @@ -19,24 +19,11 @@ * under the License. */ -import java.io.File; - -import org.apache.maven.RepositoryUtils; -import org.apache.maven.artifact.Artifact; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Component; 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.DefaultArtifact; -import org.eclipse.aether.installation.InstallRequest; -import org.eclipse.aether.installation.InstallationException; -import org.eclipse.aether.util.artifact.SubArtifact; /** * Common fields for installation mojos. @@ -49,105 +36,9 @@ public abstract class AbstractInstallMojo @Component protected RepositorySystem repositorySystem; + @Component + protected Installer installer; + @Parameter( defaultValue = "${session}", required = true, readonly = true ) protected MavenSession session; - - /** - * Gets the path of the specified artifact within the local repository. Note that the returned path need not exist - * (yet). - * - * @param artifact The artifact whose local repo path should be determined, must not be null. - * @return The absolute path to the artifact when installed, never null. - */ - protected File getLocalRepoFile( Artifact artifact ) - { - String path = session.getRepositorySession().getLocalRepositoryManager() - .getPathForLocalArtifact( RepositoryUtils.toArtifact( artifact ) ); - return new File( session.getRepositorySession().getLocalRepository().getBasedir(), path ); - } - - /** - * Gets the path of the specified artifact metadata within the local repository. Note that the returned path need - * not exist (yet). - * - * @param metadata The artifact metadata whose local repo path should be determined, must not be null. - * @return The absolute path to the artifact metadata when installed, never null. - */ - protected File getLocalRepoFile( ProjectArtifactMetadata metadata ) - { - DefaultArtifact pomArtifact = new DefaultArtifact( - metadata.getGroupId(), - metadata.getArtifactId(), - "", - "pom", - metadata.getBaseVersion() ); - - String path = session.getRepositorySession().getLocalRepositoryManager().getPathForLocalArtifact( - pomArtifact ); - return new File( session.getRepositorySession().getLocalRepository().getBasedir(), path ); - } - - protected void installProject( MavenProject project ) - throws MojoFailureException, MojoExecutionException - { - try - { - InstallRequest request = new InstallRequest(); - Artifact artifact = project.getArtifact(); - String packaging = project.getPackaging(); - File pomFile = project.getFile(); - boolean isPomArtifact = "pom".equals( packaging ); - - if ( pomFile != null ) - { - request.addArtifact( RepositoryUtils.toArtifact( new ProjectArtifact( project ) ) ); - } - - if ( !isPomArtifact ) - { - File file = artifact.getFile(); - - // Here, we have a temporary solution to MINSTALL-3 (isDirectory() is true if it went through compile - // but not package). We are designing in a proper solution for Maven 2.1 - if ( file != null && file.isFile() ) - { - org.eclipse.aether.artifact.Artifact mainArtifact = RepositoryUtils.toArtifact( artifact ); - request.addArtifact( mainArtifact ); - - for ( Object metadata : artifact.getMetadataList() ) - { - if ( metadata instanceof ProjectArtifactMetadata ) - { - org.eclipse.aether.artifact.Artifact pomArtifact = - new SubArtifact( mainArtifact, "", "pom" ); - pomArtifact = pomArtifact.setFile( ( (ProjectArtifactMetadata) metadata ).getFile() ); - request.addArtifact( pomArtifact ); - } - } - } - else if ( !project.getAttachedArtifacts().isEmpty() ) - { - 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 MojoExecutionException( "The packaging for this project did not assign " - + "a file to the build artifact" ); - } - } - - for ( Artifact attached : project.getAttachedArtifacts() ) - { - getLog().debug( "Attaching for install: " + attached.getId() ); - request.addArtifact( RepositoryUtils.toArtifact( attached ) ); - } - - repositorySystem.install( session.getRepositorySession(), request ); - } - catch ( InstallationException e ) - { - throw new MojoExecutionException( e.getMessage(), e ); - } - } } diff --git a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java index c6d527d7..5591631e 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java @@ -32,28 +32,14 @@ import java.util.jar.JarFile; import java.util.regex.Pattern; -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.handler.DefaultArtifactHandler; -import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Model; import org.apache.maven.model.Parent; -import org.apache.maven.model.building.ModelBuildingException; -import org.apache.maven.model.building.ModelSource; -import org.apache.maven.model.building.StringModelSource; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.model.io.xpp3.MavenXpp3Writer; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.project.DefaultProjectBuildingRequest; -import org.apache.maven.project.MavenProject; -import org.apache.maven.project.MavenProjectHelper; -import org.apache.maven.project.ProjectBuilder; -import org.apache.maven.project.ProjectBuildingException; -import org.apache.maven.project.ProjectBuildingRequest; -import org.apache.maven.project.artifact.ProjectArtifactMetadata; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.xml.XmlStreamReader; @@ -61,8 +47,16 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import org.eclipse.aether.DefaultRepositoryCache; import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.artifact.ArtifactType; +import org.eclipse.aether.artifact.DefaultArtifact; +import org.eclipse.aether.artifact.DefaultArtifactType; +import org.eclipse.aether.installation.InstallRequest; +import org.eclipse.aether.installation.InstallationException; import org.eclipse.aether.repository.LocalRepository; import org.eclipse.aether.repository.LocalRepositoryManager; +import org.eclipse.aether.util.artifact.SubArtifact; /** * Installs a file in the local repository. @@ -161,21 +155,7 @@ public class InstallFileMojo @Parameter( property = "localRepositoryPath" ) private File localRepositoryPath; - /** - * Used for attaching the artifacts to install to the project. - */ - @Component - private MavenProjectHelper projectHelper; - - /** - * Used for creating the project to which the artifacts to install will be attached. - */ - @Component - private ProjectBuilder projectBuilder; - - /** - * @see org.apache.maven.plugin.Mojo#execute() - */ + @Override public void execute() throws MojoExecutionException, MojoFailureException { @@ -186,6 +166,7 @@ public void execute() throw new MojoFailureException( message ); } + RepositorySystemSession repositorySystemSession = session.getRepositorySession(); if ( localRepositoryPath != null ) { // "clone" repository session and replace localRepository @@ -202,8 +183,7 @@ public void execute() LocalRepositoryManager localRepositoryManager = repositorySystem.newLocalRepositoryManager( newSession, new LocalRepository( localRepositoryPath, contentType ) ); newSession.setLocalRepositoryManager( localRepositoryManager ); - this.session = new MavenSession( - session.getContainer(), newSession, session.getRequest(), session.getResult() ); + repositorySystemSession = newSession; } File temporaryPom = null; @@ -218,65 +198,54 @@ public void execute() pomFile = temporaryPom; } - MavenProject project = createMavenProject(); - - // We need to set a new ArtifactHandler otherwise - // the extension will be set to the packaging type - // which is sometimes wrong. - DefaultArtifactHandler ah = new DefaultArtifactHandler( packaging ); - ah.setExtension( FileUtils.getExtension( file.getName() ) ); - - project.getArtifact().setArtifactHandler( ah ); - Artifact artifact = project.getArtifact(); - - if ( file.equals( getLocalRepoFile( artifact ) ) ) + if ( groupId == null || artifactId == null || version == null || packaging == null ) { - throw new MojoFailureException( "Cannot install artifact. " - + "Artifact is already in the local repository." + LS + LS + "File in question is: " + file + LS ); + throw new MojoExecutionException( "The artifact information is incomplete: 'groupId', 'artifactId', " + + "'version' and 'packaging' are required." ); } - if ( classifier == null ) + InstallRequest installRequest = new InstallRequest(); + + ArtifactType artifactType = session.getRepositorySession().getArtifactTypeRegistry().get( packaging ); + if ( artifactType == null ) { - artifact.setFile( file ); - if ( "pom".equals( packaging ) ) - { - project.setFile( file ); - } + artifactType = new DefaultArtifactType( + packaging, FileUtils.getExtension( file.getName() ), classifier, "none" + ); } - else + + Artifact mainArtifact = new DefaultArtifact( + groupId, + artifactId, + classifier, + null, + version, + artifactType + ).setFile( file ); + installRequest.addArtifact( mainArtifact ); + + File artifactLocalFile = installer.getLocalRepositoryFile( session.getRepositorySession(), mainArtifact ); + File pomLocalFile = installer.getPomLocalRepositoryFile( session.getRepositorySession(), mainArtifact ); + + if ( file.equals( artifactLocalFile ) ) { - projectHelper.attachArtifact( project, packaging, classifier, file ); + throw new MojoFailureException( "Cannot install artifact. " + + "Artifact is already in the local repository." + LS + LS + "File in question is: " + file + LS ); } if ( !"pom".equals( packaging ) ) { if ( pomFile != null ) { - if ( classifier == null ) - { - artifact.addMetadata( new ProjectArtifactMetadata( artifact, pomFile ) ); - } - else - { - project.setFile( pomFile ); - } + installRequest.addArtifact( new SubArtifact( mainArtifact, "", "pom", pomFile ) ); } else { - temporaryPom = generatePomFile(); - ProjectArtifactMetadata pomMetadata = new ProjectArtifactMetadata( artifact, temporaryPom ); - if ( Boolean.TRUE.equals( generatePom ) - || ( generatePom == null && !getLocalRepoFile( pomMetadata ).exists() ) ) + if ( Boolean.TRUE.equals( generatePom ) || ( generatePom == null && !pomLocalFile.exists() ) ) { + temporaryPom = generatePomFile(); getLog().debug( "Installing generated POM" ); - if ( classifier == null ) - { - artifact.addMetadata( pomMetadata ); - } - else - { - project.setFile( temporaryPom ); - } + installRequest.addArtifact( new SubArtifact( mainArtifact, "", "pom", temporaryPom ) ); } else if ( generatePom == null ) { @@ -287,19 +256,19 @@ else if ( generatePom == null ) if ( sources != null ) { - projectHelper.attachArtifact( project, "jar", "sources", sources ); + installRequest.addArtifact( new SubArtifact( mainArtifact, "sources", "jar", sources ) ); } if ( javadoc != null ) { - projectHelper.attachArtifact( project, "jar", "javadoc", javadoc ); + installRequest.addArtifact( new SubArtifact( mainArtifact, "javadoc", "jar", javadoc ) ); } try { - installProject( project ); + repositorySystem.install( repositorySystemSession, installRequest ); } - catch ( Exception e ) + catch ( InstallationException e ) { throw new MojoExecutionException( e.getMessage(), e ); } @@ -313,43 +282,6 @@ else if ( generatePom == null ) } } - /** - * Creates a Maven project in-memory from the user-supplied groupId, artifactId and version. When a classifier is - * supplied, the packaging must be POM because the project with only have attachments. This project serves as basis - * to attach the artifacts to install to. - * - * @return The created Maven project, never null. - * @throws MojoExecutionException When the model of the project could not be built. - * @throws MojoFailureException When building the project failed. - */ - private MavenProject createMavenProject() - throws MojoExecutionException, MojoFailureException - { - if ( groupId == null || artifactId == null || version == null || packaging == null ) - { - throw new MojoExecutionException( "The artifact information is incomplete: 'groupId', 'artifactId', " - + "'version' and 'packaging' are required." ); - } - ModelSource modelSource = new StringModelSource( "4.0.0" - + groupId + "" + artifactId + "" + version - + "" + ( classifier == null ? packaging : "pom" ) + "" ); - ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() ); - pbr.setProcessPlugins( false ); - try - { - return projectBuilder.build( modelSource, pbr ).getProject(); - } - catch ( ProjectBuildingException e ) - { - if ( e.getCause() instanceof ModelBuildingException ) - { - throw new MojoExecutionException( "The artifact information is not valid:" + LS - + e.getCause().getMessage() ); - } - throw new MojoFailureException( "Unable to create the project.", e ); - } - } - private File readingPomFromJarFile() throws MojoExecutionException { diff --git a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java index 51d15cea..f01d6c90 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java @@ -29,6 +29,7 @@ import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; +import org.eclipse.aether.installation.InstallationException; /** * Installs the project's main artifact, and any other artifacts attached by other plugins in the lifecycle, to the @@ -92,6 +93,7 @@ private boolean hasState( MavenProject project ) return pluginContext.containsKey( INSTALL_PROCESSED_MARKER ); } + @Override public void execute() throws MojoExecutionException, MojoFailureException { @@ -147,6 +149,22 @@ private boolean allProjectsMarked() return true; } + private void installProject( MavenProject project ) throws MojoExecutionException, MojoFailureException + { + try + { + repositorySystem.install( session.getRepositorySession(), installer.processProject( project ) ); + } + catch ( IllegalArgumentException e ) + { + throw new MojoFailureException( e.getMessage(), e ); + } + catch ( InstallationException e ) + { + throw new MojoExecutionException( e.getMessage(), e ); + } + } + public void setSkip( boolean skip ) { this.skip = skip; diff --git a/src/main/java/org/apache/maven/plugins/install/Installer.java b/src/main/java/org/apache/maven/plugins/install/Installer.java new file mode 100644 index 00000000..66568a25 --- /dev/null +++ b/src/main/java/org/apache/maven/plugins/install/Installer.java @@ -0,0 +1,143 @@ +package org.apache.maven.plugins.install; + +/* + * 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 javax.inject.Named; +import javax.inject.Singleton; + +import java.io.File; + +import org.apache.maven.RepositoryUtils; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.artifact.ProjectArtifact; +import org.apache.maven.project.artifact.ProjectArtifactMetadata; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.installation.InstallRequest; +import org.eclipse.aether.util.artifact.SubArtifact; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Installer component. + */ +@Singleton +@Named +public class Installer +{ + private final Logger logger = LoggerFactory.getLogger( getClass() ); + + /** + * Gets the path of the specified artifact within the local repository. Note that the returned path need not exist + * (yet). + * + * @param artifact The artifact whose local repo path should be determined, must not be null. + * @return The absolute path to the artifact when installed, never null. + */ + public File getLocalRepositoryFile( RepositorySystemSession session, Artifact artifact ) + { + String path = session.getLocalRepositoryManager().getPathForLocalArtifact( artifact ); + return new File( session.getLocalRepository().getBasedir(), path ); + } + + /** + * Gets the path of the specified artifact POM within the local repository. Note that the returned path need + * not exist (yet). + * + * @param artifact The artifact whose POM local repo path should be determined, must not be null. + * @return The absolute path to the artifact POM when installed, never null. + */ + public File getPomLocalRepositoryFile( RepositorySystemSession session, Artifact artifact ) + { + SubArtifact pomArtifact = new SubArtifact( artifact, "", "pom" ); + String path = session.getLocalRepositoryManager().getPathForLocalArtifact( pomArtifact ); + return new File( session.getLocalRepository().getBasedir(), path ); + } + + /** + * Processes passed in {@link MavenProject} and produces {@link InstallRequest} out of it. + * + * @throws IllegalArgumentException if project is badly set up. + */ + public InstallRequest processProject( MavenProject project ) + { + 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 ) + { + request.addArtifact( RepositoryUtils.toArtifact( new ProjectArtifact( project ) ) ); + pomArtifactAttached = true; + } + + if ( !isPomArtifact ) + { + File file = mavenMainArtifact.getFile(); + if ( file != null && file.isFile() ) + { + 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; + } + } + } + } + else if ( !project.getAttachedArtifacts().isEmpty() ) + { + throw new IllegalArgumentException( "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 " + + "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() ) + { + logger.debug( "Attaching for install: " + attached.getId() ); + request.addArtifact( RepositoryUtils.toArtifact( attached ) ); + } + + return request; + } +} diff --git a/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java b/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java index c135d959..9ed4b6e1 100644 --- a/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java @@ -31,9 +31,11 @@ import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.xml.XmlStreamReader; import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.artifact.DefaultArtifactType; import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory; import org.eclipse.aether.repository.LocalRepository; import org.eclipse.aether.repository.NoLocalRepositoryManagerException; +import org.eclipse.aether.util.artifact.DefaultArtifactTypeRegistry; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -291,6 +293,9 @@ private MavenSession createMavenSession() throws NoLocalRepositoryManagerExcepti repositorySession, new LocalRepository( LOCAL_REPO ) ) ); + DefaultArtifactTypeRegistry stereotypes = new DefaultArtifactTypeRegistry(); + stereotypes.add( new DefaultArtifactType( "pom" ) ); + repositorySession.setArtifactTypeRegistry( stereotypes ); ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); buildingRequest.setRepositorySession( repositorySession ); when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest ); diff --git a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java index 6fea2892..6da54a8c 100644 --- a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java @@ -32,6 +32,7 @@ import org.apache.maven.artifact.metadata.ArtifactMetadata; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.apache.maven.plugins.install.stubs.AttachedArtifactStub0; @@ -214,7 +215,7 @@ public void testInstallIfArtifactFileIsNull() fail( "Did not throw mojo execution exception" ); } - catch ( MojoExecutionException e ) + catch ( MojoFailureException e ) { //expected } From 81a6446c00341d931b1b1ceedcf4e4089128a5aa Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Thu, 7 Jul 2022 11:59:35 +0200 Subject: [PATCH 05/10] Fix issues --- .../plugins/install/InstallFileMojo.java | 30 ++++++++----- .../maven/plugins/install/Installer.java | 42 +++++++++++++++++++ .../plugins/install/InstallFileMojoTest.java | 5 --- 3 files changed, 62 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java index 5591631e..dc5b944a 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java @@ -42,6 +42,7 @@ import org.apache.maven.plugins.annotations.Parameter; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.xml.XmlStreamReader; import org.codehaus.plexus.util.xml.XmlStreamWriter; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; @@ -51,7 +52,6 @@ import org.eclipse.aether.artifact.Artifact; import org.eclipse.aether.artifact.ArtifactType; import org.eclipse.aether.artifact.DefaultArtifact; -import org.eclipse.aether.artifact.DefaultArtifactType; import org.eclipse.aether.installation.InstallRequest; import org.eclipse.aether.installation.InstallationException; import org.eclipse.aether.repository.LocalRepository; @@ -184,6 +184,7 @@ public void execute() new LocalRepository( localRepositoryPath, contentType ) ); newSession.setLocalRepositoryManager( localRepositoryManager ); repositorySystemSession = newSession; + getLog().debug( "localRepoPath: " + localRepositoryManager.getRepository().getBasedir() ); } File temporaryPom = null; @@ -204,23 +205,32 @@ public void execute() + "'version' and 'packaging' are required." ); } + if ( !installer.isValidId( groupId ) + || !installer.isValidId( artifactId ) + || !installer.isValidVersion( version ) ) + { + throw new MojoExecutionException( "The artifact information is not valid: uses invalid characters." ); + } + InstallRequest installRequest = new InstallRequest(); - ArtifactType artifactType = session.getRepositorySession().getArtifactTypeRegistry().get( packaging ); - if ( artifactType == null ) + boolean isFilePom = classifier == null && "pom".equals( packaging ); + if ( !isFilePom ) { - artifactType = new DefaultArtifactType( - packaging, FileUtils.getExtension( file.getName() ), classifier, "none" - ); + ArtifactType artifactType = repositorySystemSession.getArtifactTypeRegistry().get( packaging ); + if ( artifactType != null + && StringUtils.isEmpty( classifier ) + && !StringUtils.isEmpty( artifactType.getClassifier() ) ) + { + classifier = artifactType.getClassifier(); + } } - Artifact mainArtifact = new DefaultArtifact( groupId, artifactId, classifier, - null, - version, - artifactType + isFilePom ? "pom" : FileUtils.getExtension( file.getName() ), + version ).setFile( file ); installRequest.addArtifact( mainArtifact ); diff --git a/src/main/java/org/apache/maven/plugins/install/Installer.java b/src/main/java/org/apache/maven/plugins/install/Installer.java index 66568a25..f5c03956 100644 --- a/src/main/java/org/apache/maven/plugins/install/Installer.java +++ b/src/main/java/org/apache/maven/plugins/install/Installer.java @@ -140,4 +140,46 @@ else if ( !project.getAttachedArtifacts().isEmpty() ) return request; } + + public boolean isValidId( String id ) + { + if ( id == null ) + { + return false; + } + for ( int i = 0; i < id.length(); i++ ) + { + char c = id.charAt( i ); + if ( !isValidIdCharacter( c ) ) + { + return false; + } + } + return true; + } + + + private boolean isValidIdCharacter( char c ) + { + return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '-' || c == '_' || c == '.'; + } + + private static final String ILLEGAL_VERSION_CHARS = "\\/:\"<>|?*[](){},"; + + public boolean isValidVersion( String version ) + { + if ( version == null ) + { + return false; + } + for ( int i = version.length() - 1; i >= 0; i-- ) + { + if ( ILLEGAL_VERSION_CHARS.indexOf( version.charAt( i ) ) >= 0 ) + { + return false; + } + } + return true; + } + } diff --git a/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java b/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java index 9ed4b6e1..c135d959 100644 --- a/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/install/InstallFileMojoTest.java @@ -31,11 +31,9 @@ import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.xml.XmlStreamReader; import org.eclipse.aether.DefaultRepositorySystemSession; -import org.eclipse.aether.artifact.DefaultArtifactType; import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory; import org.eclipse.aether.repository.LocalRepository; import org.eclipse.aether.repository.NoLocalRepositoryManagerException; -import org.eclipse.aether.util.artifact.DefaultArtifactTypeRegistry; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -293,9 +291,6 @@ private MavenSession createMavenSession() throws NoLocalRepositoryManagerExcepti repositorySession, new LocalRepository( LOCAL_REPO ) ) ); - DefaultArtifactTypeRegistry stereotypes = new DefaultArtifactTypeRegistry(); - stereotypes.add( new DefaultArtifactType( "pom" ) ); - repositorySession.setArtifactTypeRegistry( stereotypes ); ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(); buildingRequest.setRepositorySession( repositorySession ); when( session.getProjectBuildingRequest() ).thenReturn( buildingRequest ); From 36a0405ded0f3cc73702c5f1e1196361ed469eec Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Thu, 7 Jul 2022 12:04:12 +0200 Subject: [PATCH 06/10] Collapse --- .../plugins/install/AbstractInstallMojo.java | 152 +++++++++++++- .../plugins/install/InstallFileMojo.java | 10 +- .../maven/plugins/install/InstallMojo.java | 2 +- .../maven/plugins/install/Installer.java | 185 ------------------ 4 files changed, 155 insertions(+), 194 deletions(-) delete mode 100644 src/main/java/org/apache/maven/plugins/install/Installer.java diff --git a/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java b/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java index 28aa4f36..6aefc6c3 100644 --- a/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java @@ -19,11 +19,21 @@ * under the License. */ +import java.io.File; + +import org.apache.maven.RepositoryUtils; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugins.annotations.Component; 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.RepositorySystemSession; +import org.eclipse.aether.artifact.Artifact; +import org.eclipse.aether.installation.InstallRequest; +import org.eclipse.aether.util.artifact.SubArtifact; /** * Common fields for installation mojos. @@ -36,9 +46,145 @@ public abstract class AbstractInstallMojo @Component protected RepositorySystem repositorySystem; - @Component - protected Installer installer; - @Parameter( defaultValue = "${session}", required = true, readonly = true ) protected MavenSession session; + + // this below smells like API-like thing + + /** + * Gets the path of the specified artifact within the local repository. Note that the returned path need not exist + * (yet). + * + * @param artifact The artifact whose local repo path should be determined, must not be null. + * @return The absolute path to the artifact when installed, never null. + */ + protected File getLocalRepositoryFile( RepositorySystemSession session, Artifact artifact ) + { + String path = session.getLocalRepositoryManager().getPathForLocalArtifact( artifact ); + return new File( session.getLocalRepository().getBasedir(), path ); + } + + /** + * Gets the path of the specified artifact POM within the local repository. Note that the returned path need + * not exist (yet). + * + * @param artifact The artifact whose POM local repo path should be determined, must not be null. + * @return The absolute path to the artifact POM when installed, never null. + */ + protected File getPomLocalRepositoryFile( RepositorySystemSession session, Artifact artifact ) + { + SubArtifact pomArtifact = new SubArtifact( artifact, "", "pom" ); + String path = session.getLocalRepositoryManager().getPathForLocalArtifact( pomArtifact ); + return new File( session.getLocalRepository().getBasedir(), path ); + } + + /** + * Processes passed in {@link MavenProject} and produces {@link InstallRequest} out of it. + * + * @throws IllegalArgumentException if project is badly set up. + */ + protected InstallRequest processProject( MavenProject project ) + { + 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 ) + { + request.addArtifact( RepositoryUtils.toArtifact( new ProjectArtifact( project ) ) ); + pomArtifactAttached = true; + } + + if ( !isPomArtifact ) + { + File file = mavenMainArtifact.getFile(); + if ( file != null && file.isFile() ) + { + 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; + } + } + } + } + else if ( !project.getAttachedArtifacts().isEmpty() ) + { + throw new IllegalArgumentException( "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 " + + "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() ) + { + getLog().debug( "Attaching for install: " + attached.getId() ); + request.addArtifact( RepositoryUtils.toArtifact( attached ) ); + } + + return request; + } + + protected boolean isValidId( String id ) + { + if ( id == null ) + { + return false; + } + for ( int i = 0; i < id.length(); i++ ) + { + char c = id.charAt( i ); + if ( !isValidIdCharacter( c ) ) + { + return false; + } + } + return true; + } + + private boolean isValidIdCharacter( char c ) + { + return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '-' || c == '_' || c == '.'; + } + + private static final String ILLEGAL_VERSION_CHARS = "\\/:\"<>|?*[](){},"; + + protected boolean isValidVersion( String version ) + { + if ( version == null ) + { + return false; + } + for ( int i = version.length() - 1; i >= 0; i-- ) + { + if ( ILLEGAL_VERSION_CHARS.indexOf( version.charAt( i ) ) >= 0 ) + { + return false; + } + } + return true; + } } diff --git a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java index dc5b944a..af6c0694 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java @@ -205,9 +205,9 @@ public void execute() + "'version' and 'packaging' are required." ); } - if ( !installer.isValidId( groupId ) - || !installer.isValidId( artifactId ) - || !installer.isValidVersion( version ) ) + if ( !isValidId( groupId ) + || !isValidId( artifactId ) + || !isValidVersion( version ) ) { throw new MojoExecutionException( "The artifact information is not valid: uses invalid characters." ); } @@ -234,8 +234,8 @@ public void execute() ).setFile( file ); installRequest.addArtifact( mainArtifact ); - File artifactLocalFile = installer.getLocalRepositoryFile( session.getRepositorySession(), mainArtifact ); - File pomLocalFile = installer.getPomLocalRepositoryFile( session.getRepositorySession(), mainArtifact ); + File artifactLocalFile = getLocalRepositoryFile( session.getRepositorySession(), mainArtifact ); + File pomLocalFile = getPomLocalRepositoryFile( session.getRepositorySession(), mainArtifact ); if ( file.equals( artifactLocalFile ) ) { diff --git a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java index f01d6c90..245ce7a2 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java @@ -153,7 +153,7 @@ private void installProject( MavenProject project ) throws MojoExecutionExceptio { try { - repositorySystem.install( session.getRepositorySession(), installer.processProject( project ) ); + repositorySystem.install( session.getRepositorySession(), processProject( project ) ); } catch ( IllegalArgumentException e ) { diff --git a/src/main/java/org/apache/maven/plugins/install/Installer.java b/src/main/java/org/apache/maven/plugins/install/Installer.java deleted file mode 100644 index f5c03956..00000000 --- a/src/main/java/org/apache/maven/plugins/install/Installer.java +++ /dev/null @@ -1,185 +0,0 @@ -package org.apache.maven.plugins.install; - -/* - * 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 javax.inject.Named; -import javax.inject.Singleton; - -import java.io.File; - -import org.apache.maven.RepositoryUtils; -import org.apache.maven.project.MavenProject; -import org.apache.maven.project.artifact.ProjectArtifact; -import org.apache.maven.project.artifact.ProjectArtifactMetadata; -import org.eclipse.aether.RepositorySystemSession; -import org.eclipse.aether.artifact.Artifact; -import org.eclipse.aether.installation.InstallRequest; -import org.eclipse.aether.util.artifact.SubArtifact; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Installer component. - */ -@Singleton -@Named -public class Installer -{ - private final Logger logger = LoggerFactory.getLogger( getClass() ); - - /** - * Gets the path of the specified artifact within the local repository. Note that the returned path need not exist - * (yet). - * - * @param artifact The artifact whose local repo path should be determined, must not be null. - * @return The absolute path to the artifact when installed, never null. - */ - public File getLocalRepositoryFile( RepositorySystemSession session, Artifact artifact ) - { - String path = session.getLocalRepositoryManager().getPathForLocalArtifact( artifact ); - return new File( session.getLocalRepository().getBasedir(), path ); - } - - /** - * Gets the path of the specified artifact POM within the local repository. Note that the returned path need - * not exist (yet). - * - * @param artifact The artifact whose POM local repo path should be determined, must not be null. - * @return The absolute path to the artifact POM when installed, never null. - */ - public File getPomLocalRepositoryFile( RepositorySystemSession session, Artifact artifact ) - { - SubArtifact pomArtifact = new SubArtifact( artifact, "", "pom" ); - String path = session.getLocalRepositoryManager().getPathForLocalArtifact( pomArtifact ); - return new File( session.getLocalRepository().getBasedir(), path ); - } - - /** - * Processes passed in {@link MavenProject} and produces {@link InstallRequest} out of it. - * - * @throws IllegalArgumentException if project is badly set up. - */ - public InstallRequest processProject( MavenProject project ) - { - 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 ) - { - request.addArtifact( RepositoryUtils.toArtifact( new ProjectArtifact( project ) ) ); - pomArtifactAttached = true; - } - - if ( !isPomArtifact ) - { - File file = mavenMainArtifact.getFile(); - if ( file != null && file.isFile() ) - { - 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; - } - } - } - } - else if ( !project.getAttachedArtifacts().isEmpty() ) - { - throw new IllegalArgumentException( "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 " - + "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() ) - { - logger.debug( "Attaching for install: " + attached.getId() ); - request.addArtifact( RepositoryUtils.toArtifact( attached ) ); - } - - return request; - } - - public boolean isValidId( String id ) - { - if ( id == null ) - { - return false; - } - for ( int i = 0; i < id.length(); i++ ) - { - char c = id.charAt( i ); - if ( !isValidIdCharacter( c ) ) - { - return false; - } - } - return true; - } - - - private boolean isValidIdCharacter( char c ) - { - return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '-' || c == '_' || c == '.'; - } - - private static final String ILLEGAL_VERSION_CHARS = "\\/:\"<>|?*[](){},"; - - public boolean isValidVersion( String version ) - { - if ( version == null ) - { - return false; - } - for ( int i = version.length() - 1; i >= 0; i-- ) - { - if ( ILLEGAL_VERSION_CHARS.indexOf( version.charAt( i ) ) >= 0 ) - { - return false; - } - } - return true; - } - -} From e4ac9389dc0337361536293e91c3185c38cfcb9c Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Thu, 7 Jul 2022 12:25:56 +0200 Subject: [PATCH 07/10] Javadoc fixes. --- .../maven/plugins/install/AbstractInstallMojo.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java b/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java index 6aefc6c3..f125e2fa 100644 --- a/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java @@ -54,9 +54,6 @@ public abstract class AbstractInstallMojo /** * Gets the path of the specified artifact within the local repository. Note that the returned path need not exist * (yet). - * - * @param artifact The artifact whose local repo path should be determined, must not be null. - * @return The absolute path to the artifact when installed, never null. */ protected File getLocalRepositoryFile( RepositorySystemSession session, Artifact artifact ) { @@ -67,9 +64,6 @@ protected File getLocalRepositoryFile( RepositorySystemSession session, Artifact /** * Gets the path of the specified artifact POM within the local repository. Note that the returned path need * not exist (yet). - * - * @param artifact The artifact whose POM local repo path should be determined, must not be null. - * @return The absolute path to the artifact POM when installed, never null. */ protected File getPomLocalRepositoryFile( RepositorySystemSession session, Artifact artifact ) { @@ -148,6 +142,9 @@ else if ( !project.getAttachedArtifacts().isEmpty() ) return request; } + /** + * Returns {@code true} if passed in string is "valid Maven ID" (groupId or artifactId). + */ protected boolean isValidId( String id ) { if ( id == null ) @@ -172,6 +169,9 @@ private boolean isValidIdCharacter( char c ) private static final String ILLEGAL_VERSION_CHARS = "\\/:\"<>|?*[](){},"; + /** + * Returns {@code true} if passed in string is "valid Maven (simple. non range, expression, etc) version". + */ protected boolean isValidVersion( String version ) { if ( version == null ) From 0ed1ad02fe7955da235cd568d8197c70db464081 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Thu, 7 Jul 2022 12:55:59 +0200 Subject: [PATCH 08/10] Tidy up --- .../org/apache/maven/plugins/install/InstallMojo.java | 8 -------- .../org/apache/maven/plugins/install/InstallMojoTest.java | 3 +-- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java index 245ce7a2..b5779cb8 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java @@ -129,9 +129,6 @@ public void execute() } } - /** - * Visible for testing. - */ private String getProjectReferenceId( MavenProject mavenProject ) { return mavenProject.getGroupId() + ":" + mavenProject.getArtifactId() + ":" + mavenProject.getVersion(); @@ -165,9 +162,4 @@ private void installProject( MavenProject project ) throws MojoExecutionExceptio } } - public void setSkip( boolean skip ) - { - this.skip = skip; - } - } diff --git a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java index 6da54a8c..602e6916 100644 --- a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java @@ -331,13 +331,12 @@ public void testSkip() setVariableValueToObject( mojo, "pluginDescriptor", new PluginDescriptor() ); setVariableValueToObject( mojo, "reactorProjects", Collections.singletonList( project ) ); setVariableValueToObject( mojo, "session", createMavenSession() ); + setVariableValueToObject( mojo, "skip", Boolean.TRUE ); artifact = (InstallArtifactStub) project.getArtifact(); artifact.setFile( file ); - mojo.setSkip( true ); - mojo.execute(); String groupId = dotToSlashReplacer( artifact.getGroupId() ); From 08419063cc59a122b7686f8c131d2a8d6b12bd27 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Thu, 7 Jul 2022 13:36:33 +0200 Subject: [PATCH 09/10] Collapse more The abstract methods was really used by "this or that", no method were used by both. The two Mojos really does not have a lot in common. --- .../plugins/install/AbstractInstallMojo.java | 190 ------------------ .../plugins/install/InstallFileMojo.java | 75 ++++++- .../maven/plugins/install/InstallMojo.java | 89 +++++++- .../plugins/install/InstallMojoTest.java | 15 +- 4 files changed, 170 insertions(+), 199 deletions(-) delete mode 100644 src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java diff --git a/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java b/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java deleted file mode 100644 index f125e2fa..00000000 --- a/src/main/java/org/apache/maven/plugins/install/AbstractInstallMojo.java +++ /dev/null @@ -1,190 +0,0 @@ -package org.apache.maven.plugins.install; - -/* - * 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 java.io.File; - -import org.apache.maven.RepositoryUtils; -import org.apache.maven.execution.MavenSession; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugins.annotations.Component; -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.RepositorySystemSession; -import org.eclipse.aether.artifact.Artifact; -import org.eclipse.aether.installation.InstallRequest; -import org.eclipse.aether.util.artifact.SubArtifact; - -/** - * Common fields for installation mojos. - * - * @author Brett Porter - */ -public abstract class AbstractInstallMojo - extends AbstractMojo -{ - @Component - protected RepositorySystem repositorySystem; - - @Parameter( defaultValue = "${session}", required = true, readonly = true ) - protected MavenSession session; - - // this below smells like API-like thing - - /** - * Gets the path of the specified artifact within the local repository. Note that the returned path need not exist - * (yet). - */ - protected File getLocalRepositoryFile( RepositorySystemSession session, Artifact artifact ) - { - String path = session.getLocalRepositoryManager().getPathForLocalArtifact( artifact ); - return new File( session.getLocalRepository().getBasedir(), path ); - } - - /** - * Gets the path of the specified artifact POM within the local repository. Note that the returned path need - * not exist (yet). - */ - protected File getPomLocalRepositoryFile( RepositorySystemSession session, Artifact artifact ) - { - SubArtifact pomArtifact = new SubArtifact( artifact, "", "pom" ); - String path = session.getLocalRepositoryManager().getPathForLocalArtifact( pomArtifact ); - return new File( session.getLocalRepository().getBasedir(), path ); - } - - /** - * Processes passed in {@link MavenProject} and produces {@link InstallRequest} out of it. - * - * @throws IllegalArgumentException if project is badly set up. - */ - protected InstallRequest processProject( MavenProject project ) - { - 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 ) - { - request.addArtifact( RepositoryUtils.toArtifact( new ProjectArtifact( project ) ) ); - pomArtifactAttached = true; - } - - if ( !isPomArtifact ) - { - File file = mavenMainArtifact.getFile(); - if ( file != null && file.isFile() ) - { - 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; - } - } - } - } - else if ( !project.getAttachedArtifacts().isEmpty() ) - { - throw new IllegalArgumentException( "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 " - + "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() ) - { - getLog().debug( "Attaching for install: " + attached.getId() ); - request.addArtifact( RepositoryUtils.toArtifact( attached ) ); - } - - return request; - } - - /** - * Returns {@code true} if passed in string is "valid Maven ID" (groupId or artifactId). - */ - protected boolean isValidId( String id ) - { - if ( id == null ) - { - return false; - } - for ( int i = 0; i < id.length(); i++ ) - { - char c = id.charAt( i ); - if ( !isValidIdCharacter( c ) ) - { - return false; - } - } - return true; - } - - private boolean isValidIdCharacter( char c ) - { - return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '-' || c == '_' || c == '.'; - } - - private static final String ILLEGAL_VERSION_CHARS = "\\/:\"<>|?*[](){},"; - - /** - * Returns {@code true} if passed in string is "valid Maven (simple. non range, expression, etc) version". - */ - protected boolean isValidVersion( String version ) - { - if ( version == null ) - { - return false; - } - for ( int i = version.length() - 1; i >= 0; i-- ) - { - if ( ILLEGAL_VERSION_CHARS.indexOf( version.charAt( i ) ) >= 0 ) - { - return false; - } - } - return true; - } -} diff --git a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java index af6c0694..31fff482 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java @@ -32,12 +32,15 @@ import java.util.jar.JarFile; import java.util.regex.Pattern; +import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Model; import org.apache.maven.model.Parent; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.model.io.xpp3.MavenXpp3Writer; +import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.codehaus.plexus.util.FileUtils; @@ -48,6 +51,7 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import org.eclipse.aether.DefaultRepositoryCache; import org.eclipse.aether.DefaultRepositorySystemSession; +import org.eclipse.aether.RepositorySystem; import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.artifact.Artifact; import org.eclipse.aether.artifact.ArtifactType; @@ -65,10 +69,16 @@ */ @Mojo( name = "install-file", requiresProject = false, aggregator = true, threadSafe = true ) public class InstallFileMojo - extends AbstractInstallMojo + extends AbstractMojo { private static final String LS = System.getProperty( "line.separator" ); + @Component + private RepositorySystem repositorySystem; + + @Parameter( defaultValue = "${session}", required = true, readonly = true ) + private MavenSession session; + /** * GroupId of the artifact to be installed. Retrieved from POM file if one is specified or extracted from * {@code pom.xml} in jar if available. @@ -503,4 +513,67 @@ private File generatePomFile() } } + /** + * Gets the path of the specified artifact within the local repository. Note that the returned path need not exist + * (yet). + */ + private File getLocalRepositoryFile( RepositorySystemSession session, Artifact artifact ) + { + String path = session.getLocalRepositoryManager().getPathForLocalArtifact( artifact ); + return new File( session.getLocalRepository().getBasedir(), path ); + } + + /** + * Gets the path of the specified artifact POM within the local repository. Note that the returned path need + * not exist (yet). + */ + private File getPomLocalRepositoryFile( RepositorySystemSession session, Artifact artifact ) + { + SubArtifact pomArtifact = new SubArtifact( artifact, "", "pom" ); + String path = session.getLocalRepositoryManager().getPathForLocalArtifact( pomArtifact ); + return new File( session.getLocalRepository().getBasedir(), path ); + } + + /** + * Returns {@code true} if passed in string is "valid Maven ID" (groupId or artifactId). + */ + private boolean isValidId( String id ) + { + if ( id == null ) + { + return false; + } + for ( int i = 0; i < id.length(); i++ ) + { + char c = id.charAt( i ); + if ( !( c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' + || c >= '0' && c <= '9' || c == '-' || c == '_' || c == '.' ) ) + { + return false; + } + } + return true; + } + + private static final String ILLEGAL_VERSION_CHARS = "\\/:\"<>|?*[](){},"; + + /** + * Returns {@code true} if passed in string is "valid Maven (simple. non range, expression, etc) version". + */ + private boolean isValidVersion( String version ) + { + if ( version == null ) + { + return false; + } + for ( int i = version.length() - 1; i >= 0; i-- ) + { + if ( ILLEGAL_VERSION_CHARS.indexOf( version.charAt( i ) ) >= 0 ) + { + return false; + } + } + return true; + } + } diff --git a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java index b5779cb8..2df634ba 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java @@ -19,17 +19,28 @@ * under the License. */ +import java.io.File; import java.util.List; import java.util.Map; +import org.apache.maven.RepositoryUtils; +import org.apache.maven.execution.MavenSession; +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 @@ -39,8 +50,14 @@ */ @Mojo( name = "install", defaultPhase = LifecyclePhase.INSTALL, threadSafe = true ) public class InstallMojo - extends AbstractInstallMojo + extends AbstractMojo { + @Component + private RepositorySystem repositorySystem; + + @Parameter( defaultValue = "${session}", required = true, readonly = true ) + private MavenSession session; + @Parameter( defaultValue = "${project}", readonly = true, required = true ) private MavenProject project; @@ -162,4 +179,74 @@ 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. + */ + private InstallRequest processProject( MavenProject project ) + { + 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 ) + { + request.addArtifact( RepositoryUtils.toArtifact( new ProjectArtifact( project ) ) ); + pomArtifactAttached = true; + } + + if ( !isPomArtifact ) + { + File file = mavenMainArtifact.getFile(); + if ( file != null && file.isFile() ) + { + 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; + } + } + } + } + else if ( !project.getAttachedArtifacts().isEmpty() ) + { + throw new IllegalArgumentException( "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 " + + "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() ) + { + getLog().debug( "Attaching for install: " + attached.getId() ); + request.addArtifact( RepositoryUtils.toArtifact( attached ) ); + } + + return request; + } + } diff --git a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java index 602e6916..57fe471c 100644 --- a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java @@ -31,6 +31,7 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.metadata.ArtifactMetadata; import org.apache.maven.execution.MavenSession; +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; @@ -72,7 +73,7 @@ public void testInstallTestEnvironment() { File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml" ); - AbstractInstallMojo mojo = (AbstractInstallMojo) lookupMojo( "install", testPom ); + AbstractMojo mojo = (AbstractMojo) lookupMojo( "install", testPom ); assertNotNull( mojo ); } @@ -82,7 +83,7 @@ public void testBasicInstall() { File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml" ); - AbstractInstallMojo mojo = (AbstractInstallMojo) lookupMojo( "install", testPom ); + AbstractMojo mojo = (AbstractMojo) lookupMojo( "install", testPom ); assertNotNull( mojo ); @@ -119,7 +120,7 @@ public void testBasicInstallWithAttachedArtifacts() File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test-with-attached-artifacts/" + "plugin-config.xml" ); - AbstractInstallMojo mojo = (AbstractInstallMojo) lookupMojo( "install", testPom ); + AbstractMojo mojo = (AbstractMojo) lookupMojo( "install", testPom ); assertNotNull( mojo ); @@ -160,7 +161,7 @@ public void testUpdateReleaseParamSetToTrue() { File testPom = new File( getBasedir(), "target/test-classes/unit/configured-install-test/plugin-config.xml" ); - AbstractInstallMojo mojo = (AbstractInstallMojo) lookupMojo( "install", testPom ); + AbstractMojo mojo = (AbstractMojo) lookupMojo( "install", testPom ); assertNotNull( mojo ); @@ -191,7 +192,7 @@ public void testInstallIfArtifactFileIsNull() { File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test/plugin-config.xml" ); - AbstractInstallMojo mojo = (AbstractInstallMojo) lookupMojo( "install", testPom ); + AbstractMojo mojo = (AbstractMojo) lookupMojo( "install", testPom ); assertNotNull( mojo ); @@ -229,7 +230,7 @@ public void testInstallIfPackagingIsPom() File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-test-packaging-pom/" + "plugin-config.xml" ); - AbstractInstallMojo mojo = (AbstractInstallMojo) lookupMojo( "install", testPom ); + AbstractMojo mojo = (AbstractMojo) lookupMojo( "install", testPom ); assertNotNull( mojo ); @@ -264,7 +265,7 @@ public void testBasicInstallAndCreate() { File testPom = new File( getBasedir(), "target/test-classes/unit/basic-install-checksum/plugin-config.xml" ); - AbstractInstallMojo mojo = (AbstractInstallMojo) lookupMojo( "install", testPom ); + AbstractMojo mojo = (AbstractMojo) lookupMojo( "install", testPom ); assertNotNull( mojo ); From f0881640c6e0d6f87002c86af9b7d15828a202e3 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Tue, 12 Jul 2022 13:12:20 +0200 Subject: [PATCH 10/10] Fix bug re combined tar file extensions --- .../invoker.properties | 18 ++++++ .../install-file-minstall-121-targz/pom.xml | 46 ++++++++++++++++ .../install-file-minstall-121-targz/setup.bsh | 29 ++++++++++ .../test-0.1.pom | 35 ++++++++++++ .../test-0.1.tar.gz | Bin 0 -> 345 bytes .../test.properties | 20 +++++++ .../verify.bsh | 52 ++++++++++++++++++ .../plugins/install/InstallFileMojo.java | 20 ++++++- .../maven/plugins/install/InstallMojo.java | 8 +-- 9 files changed, 221 insertions(+), 7 deletions(-) create mode 100644 src/it/install-file-minstall-121-targz/invoker.properties create mode 100644 src/it/install-file-minstall-121-targz/pom.xml create mode 100644 src/it/install-file-minstall-121-targz/setup.bsh create mode 100644 src/it/install-file-minstall-121-targz/test-0.1.pom create mode 100644 src/it/install-file-minstall-121-targz/test-0.1.tar.gz create mode 100644 src/it/install-file-minstall-121-targz/test.properties create mode 100644 src/it/install-file-minstall-121-targz/verify.bsh diff --git a/src/it/install-file-minstall-121-targz/invoker.properties b/src/it/install-file-minstall-121-targz/invoker.properties new file mode 100644 index 00000000..04132382 --- /dev/null +++ b/src/it/install-file-minstall-121-targz/invoker.properties @@ -0,0 +1,18 @@ +# 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. + +invoker.goals = org.apache.maven.plugins:maven-install-plugin:${project.version}:install-file diff --git a/src/it/install-file-minstall-121-targz/pom.xml b/src/it/install-file-minstall-121-targz/pom.xml new file mode 100644 index 00000000..72009e35 --- /dev/null +++ b/src/it/install-file-minstall-121-targz/pom.xml @@ -0,0 +1,46 @@ + + + + + + 4.0.0 + + org.apache.maven.its.install.121 + test-targz + 1.0 + jar + + + Test to install a file via install:install-file using + packaging and expected having a different file extension. + + + + + + org.apache.maven.plugins + maven-install-plugin + @project.version@ + + + + + diff --git a/src/it/install-file-minstall-121-targz/setup.bsh b/src/it/install-file-minstall-121-targz/setup.bsh new file mode 100644 index 00000000..44849e75 --- /dev/null +++ b/src/it/install-file-minstall-121-targz/setup.bsh @@ -0,0 +1,29 @@ +/* + * 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 java.io.*; +import java.util.*; + +import org.codehaus.plexus.util.*; + +File file = new File( localRepositoryPath, "org/apache/maven/its/install/121/test-targz" ); +System.out.println( "Deleting " + file ); +FileUtils.deleteDirectory( file ); + +return true; diff --git a/src/it/install-file-minstall-121-targz/test-0.1.pom b/src/it/install-file-minstall-121-targz/test-0.1.pom new file mode 100644 index 00000000..fe478fe5 --- /dev/null +++ b/src/it/install-file-minstall-121-targz/test-0.1.pom @@ -0,0 +1,35 @@ + + + + + + 4.0.0 + + + org.apache.maven.its.install.121 + parent + 1.0 + + + + test-targz + + diff --git a/src/it/install-file-minstall-121-targz/test-0.1.tar.gz b/src/it/install-file-minstall-121-targz/test-0.1.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..226277c382b503bdef6c6df3250c49e7f3790a61 GIT binary patch literal 345 zcmWIWW@Zs#-~d9YP4g`okN_tG3xls~h@-BjpPT-_Qw$8u3<2Kk93T};P-Ou)HH!dM zz%~0i`gyv!28ZbRx_$ONbK1vSSMMUPx31Q?Gv_x48C)@b@U%$J%U8$K_hRWP7S0(j zC67qY(9U3){!H^nnwt1i@o=%}OP+~oS3FaFo%yNgV-X|RA;MFlZ@2@k0pS2|MkWyk vRDZx63GxRjfIAH2D|D^M#)5o-0JcCTTq}~F0=!w-K&qI4uog%k192Dt?J7l_ literal 0 HcmV?d00001 diff --git a/src/it/install-file-minstall-121-targz/test.properties b/src/it/install-file-minstall-121-targz/test.properties new file mode 100644 index 00000000..8485d141 --- /dev/null +++ b/src/it/install-file-minstall-121-targz/test.properties @@ -0,0 +1,20 @@ +# 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. + +file = test-0.1.tar.gz +pomFile = test-0.1.pom +packaging = war diff --git a/src/it/install-file-minstall-121-targz/verify.bsh b/src/it/install-file-minstall-121-targz/verify.bsh new file mode 100644 index 00000000..b3f31de6 --- /dev/null +++ b/src/it/install-file-minstall-121-targz/verify.bsh @@ -0,0 +1,52 @@ +/* + * 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 java.io.*; +import java.util.*; + +String[] paths = +{ + "org/apache/maven/its/install/121/test-targz/maven-metadata-local.xml", + "org/apache/maven/its/install/121/test-targz/1.0/test-targz-1.0.pom", + "org/apache/maven/its/install/121/test-targz/1.0/test-targz-1.0.tar.gz", +}; + +for ( String path : paths ) +{ + File file = new File( localRepositoryPath, path ); + System.out.println( "Checking for existence of " + file ); + if ( !file.isFile() ) + { + throw new FileNotFoundException( "Missing: " + file.getAbsolutePath() ); + } +} + +File file = new File( basedir, "test-0.1.pom" ); +if ( !file.isFile() ) +{ + throw new FileNotFoundException( "Missing: " + file.getAbsolutePath() ); +} + +File file = new File( basedir, "test-0.1.tar.gz" ); +if ( !file.isFile() ) +{ + throw new FileNotFoundException( "Missing: " + file.getAbsolutePath() ); +} + +return true; diff --git a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java index 31fff482..aab859fa 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallFileMojo.java @@ -239,7 +239,7 @@ public void execute() groupId, artifactId, classifier, - isFilePom ? "pom" : FileUtils.getExtension( file.getName() ), + isFilePom ? "pom" : getExtension( file ), version ).setFile( file ); installRequest.addArtifact( mainArtifact ); @@ -534,6 +534,24 @@ private File getPomLocalRepositoryFile( RepositorySystemSession session, Artifac return new File( session.getLocalRepository().getBasedir(), path ); } + // these below should be shared (duplicated in m-install-p, m-deploy-p) + + /** + * Specialization of {@link FileUtils#getExtension(String)} that honors various {@code tar.xxx} combinations. + */ + private String getExtension( final File file ) + { + String filename = file.getName(); + if ( filename.contains( ".tar." ) ) + { + return "tar." + FileUtils.getExtension( filename ); + } + else + { + return FileUtils.getExtension( filename ); + } + } + /** * Returns {@code true} if passed in string is "valid Maven ID" (groupId or artifactId). */ diff --git a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java index 2df634ba..a6c694b3 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java @@ -128,7 +128,8 @@ public void execute() } else { - getLog().info( "Deferring install for " + getProjectReferenceId( project ) + " at end" ); + getLog().info( "Deferring install for " + project.getGroupId() + + ":" + project.getArtifactId() + ":" + project.getVersion() + " at end" ); putState( State.TO_BE_INSTALLED ); } } @@ -146,11 +147,6 @@ public void execute() } } - private String getProjectReferenceId( MavenProject mavenProject ) - { - return mavenProject.getGroupId() + ":" + mavenProject.getArtifactId() + ":" + mavenProject.getVersion(); - } - private boolean allProjectsMarked() { for ( MavenProject reactorProject : reactorProjects )