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