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:25 UTC

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

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 );