View Javadoc
1   package org.codehaus.mojo.appassembler;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2006-2012, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  import java.io.File;
28  import java.util.Collections;
29  import java.util.Iterator;
30  import java.util.Set;
31  
32  import org.apache.maven.artifact.Artifact;
33  import org.apache.maven.artifact.factory.ArtifactFactory;
34  import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
35  import org.apache.maven.artifact.repository.ArtifactRepository;
36  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
37  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
38  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
39  import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
40  import org.apache.maven.artifact.resolver.ArtifactResolver;
41  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
42  import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
43  import org.apache.maven.artifact.versioning.VersionRange;
44  import org.apache.maven.plugin.MojoExecutionException;
45  import org.apache.maven.plugin.MojoFailureException;
46  import org.apache.maven.plugins.annotations.Component;
47  import org.apache.maven.plugins.annotations.LifecyclePhase;
48  import org.apache.maven.plugins.annotations.Mojo;
49  import org.apache.maven.plugins.annotations.Parameter;
50  import org.apache.maven.plugins.annotations.ResolutionScope;
51  
52  /**
53   * Creates an appassembler repository. Note that this is deliberately a bit more specific than the assembly plugin
54   * version - if that could generate a flat layout and exclude JARs, it may be a suitable replacement.
55   *
56   * @author <a href="mailto:kristian.nordal@gmail.com">Kristian Nordal</a>
57   * @version $Id$
58   */
59  @Mojo( name = "create-repository", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.RUNTIME, threadSafe = true )
60  public class CreateRepositoryMojo
61      extends AbstractAppAssemblerMojo
62  {
63      // -----------------------------------------------------------------------
64      // Parameters
65      // -----------------------------------------------------------------------
66  
67      /**
68       * The directory that will be used to assemble the artifacts in and place the bin scripts.
69       */
70      @Parameter( defaultValue = "${project.build.directory}/appassembler", required = true )
71      private File assembleDirectory;
72  
73      /**
74       * Whether to install the booter artifacts into the repository. This may be needed if you are using the Shell script
75       * generators.
76       */
77      @Parameter( defaultValue = "false" )
78      private boolean installBooterArtifacts;
79  
80      /**
81       * Path (relative to <code>assembleDirectory</code>) of the desired output repository.
82       *
83       * @since 1.3.1
84       * @todo Customization doesn't work due to the shell scripts not honouring it
85       */
86      @Parameter( defaultValue = "repo" )
87      private String repositoryName;
88  
89      // -----------------------------------------------------------------------
90      // Read-only parameters
91      // -----------------------------------------------------------------------
92  
93      @Parameter( defaultValue = "${project.artifacts}", readonly = true )
94      private Set<Artifact> artifacts;
95  
96      @Parameter( defaultValue = "${plugin.version}", readonly = true )
97      private String pluginVersion;
98  
99      // -----------------------------------------------------------------------
100     // Components
101     // -----------------------------------------------------------------------
102 
103     @Component
104     private ArtifactFactory artifactFactory;
105 
106     @Component
107     private ArtifactResolver artifactResolver;
108 
109     @Component
110     private ArtifactMetadataSource metadataSource;
111 
112     // -----------------------------------------------------------------------
113     // AbstractMojo Implementation
114     // -----------------------------------------------------------------------
115 
116     /**
117      * calling from Maven.
118      *
119      * @see org.apache.maven.plugin.AbstractMojo#execute()
120      */
121     public void execute()
122         throws MojoExecutionException, MojoFailureException
123     {
124         // ----------------------------------------------------------------------
125         // Create new repository for dependencies
126         // ----------------------------------------------------------------------
127 
128         ArtifactRepositoryLayout artifactRepositoryLayout = getArtifactRepositoryLayout();
129 
130         // -----------------------------------------------------------------------
131         // Initialize
132         // -----------------------------------------------------------------------
133 
134         StringBuffer path = new StringBuffer( "file://" + assembleDirectory.getAbsolutePath() + "/" );
135 
136         path.append( repositoryName );
137 
138         ArtifactRepository artifactRepository =
139             artifactRepositoryFactory.createDeploymentArtifactRepository( "appassembler", path.toString(),
140                                                                           artifactRepositoryLayout, true );
141 
142         // -----------------------------------------------------------------------
143         // Install the project's artifact in the new repository
144         // -----------------------------------------------------------------------
145 
146         installArtifact( projectArtifact, artifactRepository );
147 
148         // ----------------------------------------------------------------------
149         // Install dependencies in the new repository
150         // ----------------------------------------------------------------------
151 
152         // TODO: merge with the artifacts below so no duplicate versions included
153         for ( Artifact artifact : artifacts )
154         {
155             installArtifact( artifact, artifactRepository, this.useTimestampInSnapshotFileName );
156         }
157 
158         if ( installBooterArtifacts )
159         {
160             // ----------------------------------------------------------------------
161             // Install appassembler booter in the new repos
162             // ----------------------------------------------------------------------
163             installBooterArtifacts( artifactRepository );
164         }
165     }
166 
167     private void installBooterArtifacts( ArtifactRepository artifactRepository )
168         throws MojoExecutionException
169     {
170         Artifact artifact =
171             artifactFactory.createDependencyArtifact( "org.codehaus.mojo.appassembler", "appassembler-booter",
172                                                       VersionRange.createFromVersion( pluginVersion ), "jar", null,
173                                                       Artifact.SCOPE_RUNTIME );
174         try
175         {
176             Artifact p =
177                 artifactFactory.createBuildArtifact( "org.codehaus.mojo.appassembler", "appassembler-maven-plugin",
178                                                      pluginVersion, "jar" );
179 
180             ArtifactFilter filter = new ExcludesArtifactFilter( Collections.singletonList( "junit:junit" ) );
181             ArtifactResolutionResult result =
182                 artifactResolver.resolveTransitively( Collections.singleton( artifact ), p, localRepository,
183                                                       Collections.EMPTY_LIST, metadataSource, filter );
184             for ( Iterator i = result.getArtifacts().iterator(); i.hasNext(); )
185             {
186                 Artifact a = (Artifact) i.next();
187                 installArtifact( a, artifactRepository, this.useTimestampInSnapshotFileName );
188             }
189         }
190         catch ( ArtifactResolutionException e )
191         {
192             throw new MojoExecutionException( "Failed to copy artifact.", e );
193         }
194         catch ( ArtifactNotFoundException e )
195         {
196             throw new MojoExecutionException( "Failed to copy artifact.", e );
197         }
198     }
199 
200 }