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

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

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