1 package org.apache.maven.plugins.repository;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
38
39
40
41
42 public class BundleCreateMojo
43 extends AbstractMojo
44 {
45 public static final String POM = "pom.xml";
46
47
48
49
50
51
52
53 private String basedir;
54
55
56
57
58
59
60
61 private MavenProject project;
62
63
64
65
66
67
68
69 private boolean disableMaterialization;
70
71
72
73
74
75
76 private JarArchiver jarArchiver;
77
78
79
80
81 private ArtifactHandlerManager artifactHandlerManager;
82
83
84
85
86 protected InputHandler inputHandler;
87
88
89
90
91
92 protected Settings settings;
93
94 public void execute()
95 throws MojoExecutionException
96 {
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
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 }