View Javadoc

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