View Javadoc
1   package org.apache.maven.plugin.coreit;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.handler.ArtifactHandler;
24  import org.apache.maven.artifact.installer.ArtifactInstallationException;
25  import org.apache.maven.artifact.installer.ArtifactInstaller;
26  import org.apache.maven.artifact.metadata.ArtifactMetadata;
27  import org.apache.maven.artifact.repository.ArtifactRepository;
28  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
29  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
30  import org.apache.maven.plugin.AbstractMojo;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugin.MojoFailureException;
33  
34  import java.io.File;
35  import java.util.List;
36  
37  /**
38   * @author Olivier Lamy
39   * @goal install-artifacts
40   * @requiresDependencyResolution runtime
41   * @phase package
42   */
43  public class InstallArtifactsMojo
44      extends AbstractMojo
45  {
46  
47      /**
48       * @readonly
49       * @parameter default-value="${project.runtimeArtifacts}"
50       */
51      private List artifacts;
52  
53      /**
54       * @component
55       */
56      private ArtifactInstaller artifactInstaller;
57  
58      /**
59       * @component
60       */
61      private ArtifactRepositoryFactory artifactRepositoryFactory;
62  
63      /**
64       * The directory that will be used to assemble the artifacts in
65       * and place the bin scripts.
66       *
67       * @required
68       * @parameter property="assembleDirectory" default-value="${project.build.directory}/appassembler"
69       */
70      private File assembleDirectory;
71  
72      /**
73       * Path (relative to assembleDirectory) of the desired output repository.
74       *
75       * @parameter default-value="repo"
76       */
77      private String repositoryName;
78  
79      public void execute()
80          throws MojoExecutionException, MojoFailureException
81      {
82  
83          ArtifactRepositoryLayout artifactRepositoryLayout = new FlatRepositoryLayout();
84  
85          // The repo where the jar files will be installed
86          ArtifactRepository artifactRepository =
87              artifactRepositoryFactory.createDeploymentArtifactRepository( "appassembler", "file://"
88                  + assembleDirectory.getAbsolutePath() + "/" + repositoryName, artifactRepositoryLayout, false );
89          for ( Object artifact1 : artifacts )
90          {
91              Artifact artifact = (Artifact) artifact1;
92  
93              installArtifact( artifactRepository, artifact );
94          }
95      }
96  
97      private void installArtifact( ArtifactRepository artifactRepository, Artifact artifact )
98          throws MojoExecutionException
99      {
100         try
101         {
102 
103             artifact.isSnapshot();
104 
105             if ( artifact.getFile() != null )
106             {
107                 artifactInstaller.install( artifact.getFile(), artifact, artifactRepository );
108             }
109         }
110         catch ( ArtifactInstallationException e )
111         {
112             throw new MojoExecutionException( "Failed to copy artifact.", e );
113         }
114     }
115 
116     /**
117      *
118      */
119     public static class FlatRepositoryLayout
120         implements ArtifactRepositoryLayout
121     {
122         private static final char ARTIFACT_SEPARATOR = '-';
123 
124         private static final char GROUP_SEPARATOR = '.';
125 
126         public String pathOf( Artifact artifact )
127         {
128             ArtifactHandler artifactHandler = artifact.getArtifactHandler();
129 
130             StringBuilder path = new StringBuilder();
131 
132             path.append( artifact.getArtifactId() ).append( ARTIFACT_SEPARATOR ).append( artifact.getVersion() );
133 
134             if ( artifact.hasClassifier() )
135             {
136                 path.append( ARTIFACT_SEPARATOR ).append( artifact.getClassifier() );
137             }
138 
139             if ( artifactHandler.getExtension() != null && artifactHandler.getExtension().length() > 0 )
140             {
141                 path.append( GROUP_SEPARATOR ).append( artifactHandler.getExtension() );
142             }
143 
144             return path.toString();
145         }
146 
147         public String pathOfLocalRepositoryMetadata( ArtifactMetadata metadata, ArtifactRepository repository )
148         {
149             return pathOfRepositoryMetadata( metadata.getLocalFilename( repository ) );
150         }
151 
152         private String pathOfRepositoryMetadata( String filename )
153         {
154             StringBuilder path = new StringBuilder();
155 
156             path.append( filename );
157 
158             return path.toString();
159         }
160 
161         public String pathOfRemoteRepositoryMetadata( ArtifactMetadata metadata )
162         {
163             return pathOfRepositoryMetadata( metadata.getRemoteFilename() );
164         }
165     }
166 
167 }