You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by cs...@apache.org on 2022/07/07 10:04:24 UTC

[maven-install-plugin] branch full-resolver updated (a32f8c2 -> 36a0405)

This is an automated email from the ASF dual-hosted git repository.

cstamas pushed a change to branch full-resolver
in repository https://gitbox.apache.org/repos/asf/maven-install-plugin.git


    from a32f8c2  Make plugin simpler
     new 81a6446  Fix issues
     new 36a0405  Collapse

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../maven/plugins/install/AbstractInstallMojo.java | 152 ++++++++++++++++++++-
 .../maven/plugins/install/InstallFileMojo.java     |  34 +++--
 .../apache/maven/plugins/install/InstallMojo.java  |   2 +-
 .../apache/maven/plugins/install/Installer.java    | 143 -------------------
 .../maven/plugins/install/InstallFileMojoTest.java |   5 -
 5 files changed, 172 insertions(+), 164 deletions(-)
 delete mode 100644 src/main/java/org/apache/maven/plugins/install/Installer.java


[maven-install-plugin] 01/02: Fix issues

Posted by cs...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

cstamas pushed a commit to branch full-resolver
in repository https://gitbox.apache.org/repos/asf/maven-install-plugin.git

commit 81a6446c00341d931b1b1ceedcf4e4089128a5aa
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Thu Jul 7 11:59:35 2022 +0200

    Fix issues
---
 .../maven/plugins/install/InstallFileMojo.java     | 30 ++++++++++------
 .../apache/maven/plugins/install/Installer.java    | 42 ++++++++++++++++++++++
 .../maven/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 5591631..dc5b944 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.Mojo;
 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.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;
@@ -184,6 +184,7 @@ public class InstallFileMojo
                     new LocalRepository( localRepositoryPath, contentType ) );
             newSession.setLocalRepositoryManager( localRepositoryManager );
             repositorySystemSession = newSession;
+            getLog().debug( "localRepoPath: " + localRepositoryManager.getRepository().getBasedir() );
         }
 
         File temporaryPom = null;
@@ -204,23 +205,32 @@ public class InstallFileMojo
                     + "'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 66568a2..f5c0395 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 @@ public class Installer
 
         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 9ed4b6e..c135d95 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.apache.maven.project.ProjectBuildingRequest;
 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 @@ public class InstallFileMojoTest
                         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 );


[maven-install-plugin] 02/02: Collapse

Posted by cs...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

cstamas pushed a commit to branch full-resolver
in repository https://gitbox.apache.org/repos/asf/maven-install-plugin.git

commit 36a0405ded0f3cc73702c5f1e1196361ed469eec
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Thu Jul 7 12:04:12 2022 +0200

    Collapse
---
 .../maven/plugins/install/AbstractInstallMojo.java | 152 ++++++++++++++++-
 .../maven/plugins/install/InstallFileMojo.java     |  10 +-
 .../apache/maven/plugins/install/InstallMojo.java  |   2 +-
 .../apache/maven/plugins/install/Installer.java    | 185 ---------------------
 4 files changed, 155 insertions(+), 194 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 28aa4f3..6aefc6c 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 @@ package org.apache.maven.plugins.install;
  * 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 <code>null</code>.
+     * @return The absolute path to the artifact when installed, never <code>null</code>.
+     */
+    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 <code>null</code>.
+     * @return The absolute path to the artifact POM when installed, never <code>null</code>.
+     */
+    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 dc5b944..af6c069 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 class InstallFileMojo
                     + "'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 class InstallFileMojo
         ).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 f01d6c9..245ce7a 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 @@ public class InstallMojo
     {
         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 f5c0395..0000000
--- 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 <code>null</code>.
-     * @return The absolute path to the artifact when installed, never <code>null</code>.
-     */
-    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 <code>null</code>.
-     * @return The absolute path to the artifact POM when installed, never <code>null</code>.
-     */
-    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;
-    }
-
-}