View Javadoc

1   package org.apache.maven.plugins.repository;
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.handler.manager.ArtifactHandlerManager;
23  import org.apache.maven.plugin.AbstractMojo;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.project.MavenProject;
26  import org.apache.maven.settings.Settings;
27  import org.codehaus.plexus.archiver.jar.JarArchiver;
28  import org.codehaus.plexus.components.interactivity.InputHandler;
29  import org.codehaus.plexus.util.StringUtils;
30  
31  import java.io.File;
32  import java.io.IOException;
33  import java.util.List;
34  
35  /**
36   * Goal which creates an upload bundle for a project built with Maven.
37   *
38   * @goal bundle-create
39   * @execute phase="package"
40   * @since 2.0
41   */
42  public class BundleCreateMojo
43      extends AbstractMojo
44  {
45      public static final String POM = "pom.xml";
46  
47      /**
48       * Base directory.
49       *
50       * @parameter default-value="${basedir}"
51       * @readonly
52       */
53      private String basedir;
54  
55      /**
56       * The current Maven project.
57       *
58       * @parameter default-value="${project}"
59       * @readonly
60       */
61      private MavenProject project;
62      
63      /**
64       * Disable validations to make sure bundle supports project materialization.
65       * <br/>
66       * <b>WARNING: This means your project will be MUCH harder to use.</b>
67       * @parameter expression="${bundle.disableMaterialization}" default-value="false"
68       */
69      private boolean disableMaterialization;
70  
71      /**
72       * Jar archiver.
73       *
74       * @component role="org.codehaus.plexus.archiver.Archiver" roleHint="jar"
75       */
76      private JarArchiver jarArchiver;
77  
78      /**
79       * @component
80       */
81      private ArtifactHandlerManager artifactHandlerManager;
82  
83      /**
84       * @component
85       */
86      protected InputHandler inputHandler;
87      
88      /**
89       * @parameter default-value="${settings}"
90       * @readonly
91       */
92      protected Settings settings;
93  
94      public void execute()
95          throws MojoExecutionException
96      {
97          // ----------------------------------------------------------------------
98          // Check the mandatory elements of the POM
99          //
100         // modelVersion
101         // groupId
102         // artifactId
103         // packaging
104         // name
105         // version
106         // description
107         // url
108         // licenses
109         // dependencies
110         // ----------------------------------------------------------------------
111 
112         // We don't have to validate modelVersion, groupId, artifactId or version here,
113         // it is done by DefaultMaven and maven-artifact
114 
115         validate( project.getName(), "project.name" );
116 
117         validate( project.getDescription(), "project.description" );
118 
119         validate( project.getUrl(), "project.url" );
120 
121         if ( project.getLicenses().isEmpty() )
122         {
123             throw new MojoExecutionException( "At least one license must be defined." );
124         }
125         
126         if ( disableMaterialization )
127         {
128             getLog().warn( "Validations to confirm support for project materialization have been DISABLED." +
129             		"\n\nYour project may not provide the POM elements necessary to allow users to retrieve sources on-demand," +
130             		"\nor to easily checkout your project in an IDE. THIS CAN SERIOUSLY INCONVENIENCE YOUR USERS." +
131             		"\n\nContinue? [y/N]" );
132             
133             try
134             {
135                 if ( 'y' != inputHandler.readLine().toLowerCase().charAt( 0 ) )
136                 {
137                     disableMaterialization = false;
138                 }
139             }
140             catch ( IOException e )
141             {
142                 getLog().debug( "Error reading confirmation: " + e.getMessage(), e );
143             }
144             
145         }
146         
147         if ( !disableMaterialization )
148         {
149             if ( project.getScm() == null )
150             {
151                 throw new MojoExecutionException( "You must supply a valid <scm>> section, with at least "
152                     + "<url> (viewing URL) and <connection> (read-only tooling connection) specified." );
153             }
154             else
155             {
156                 validate( project.getScm().getUrl(), "project.scm.url" );
157                 
158                 validate( project.getScm().getConnection(), "project.scm.url" );
159             }
160         }
161 
162         // ----------------------------------------------------------------------
163         // Create the bundle archive
164         // ----------------------------------------------------------------------
165 
166         File pom = new File( basedir, POM );
167 
168         final String finalName = project.getBuild().getFinalName();
169 
170         String outputDirectory = project.getBuild().getDirectory();
171         
172         boolean batchMode = settings == null ? false : !settings.isInteractiveMode();
173         List<File> files = BundleUtils.selectProjectFiles( new File( outputDirectory ), inputHandler, finalName, pom, getLog(), batchMode );
174 
175         String extension = artifactHandlerManager.getArtifactHandler( project.getPackaging() ).getExtension();
176 
177         File bundle = new File( outputDirectory, finalName + "-bundle.jar" );
178 
179         try
180         {
181             jarArchiver.addFile( pom, POM );
182 
183             boolean artifactChecks = !"pom".equals( project.getPackaging() );
184             boolean sourcesFound = false;
185             boolean javadocsFound = false;
186             
187             for ( File f : files )
188             {
189                 if ( artifactChecks && f.getName().endsWith( finalName + "-sources." + extension ) )
190                 {
191                     sourcesFound = true;
192                 }
193                 else if ( artifactChecks && f.getName().equals( finalName + "-javadoc." + extension ) )
194                 {
195                     javadocsFound = true;
196                 }
197                 
198                 jarArchiver.addFile( f, f.getName() );
199             }
200             
201             if ( artifactChecks && !sourcesFound )
202             {
203                 getLog().warn( "Sources not included in upload bundle. In order to add sources please run"
204                     + " \"mvn source:jar javadoc:jar repository:bundle-create\"" );
205             }
206 
207             if ( artifactChecks && !javadocsFound )
208             {
209                 getLog().warn( "Javadoc not included in upload bundle. In order to add javadocs please run"
210                     + " \"mvn source:jar javadoc:jar repository:bundle-create\"" );
211             }
212 
213             jarArchiver.setDestFile( bundle );
214 
215             jarArchiver.createArchive();
216         }
217         catch ( Exception e )
218         {
219             e.printStackTrace();
220 
221             throw new MojoExecutionException( "Error creating upload bundle archive." );
222         }
223     }
224 
225     private void validate( String data, String expression )
226         throws MojoExecutionException
227     {
228         if ( StringUtils.isEmpty( data ) )
229         {
230             throw new MojoExecutionException( expression + " must be present." );
231         }
232     }
233 }