1 package org.codehaus.mojo.appassembler;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import java.io.File;
28 import java.io.IOException;
29
30 import org.apache.maven.artifact.Artifact;
31 import org.apache.maven.artifact.repository.ArtifactRepository;
32 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
33 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
34 import org.apache.maven.plugin.AbstractMojo;
35 import org.apache.maven.plugin.MojoExecutionException;
36 import org.apache.maven.plugin.MojoFailureException;
37 import org.apache.maven.plugins.annotations.Component;
38 import org.apache.maven.plugins.annotations.Parameter;
39 import org.apache.maven.shared.mapping.MappingUtils;
40 import org.codehaus.plexus.PlexusConstants;
41 import org.codehaus.plexus.PlexusContainer;
42 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
43 import org.codehaus.plexus.context.Context;
44 import org.codehaus.plexus.context.ContextException;
45 import org.codehaus.plexus.interpolation.InterpolationException;
46 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
47 import org.codehaus.plexus.util.FileUtils;
48
49
50
51
52
53
54 public abstract class AbstractAppAssemblerMojo
55 extends AbstractMojo
56 implements Contextualizable
57 {
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 @Parameter
73 protected String outputFileNameMapping;
74
75
76
77
78
79
80 @Parameter( defaultValue = "default" )
81 protected String repositoryLayout;
82
83
84
85
86
87
88 @Parameter( defaultValue = "true" )
89 protected boolean useTimestampInSnapshotFileName;
90
91
92
93
94
95 @Parameter( defaultValue = "${localRepository}", readonly = true )
96 protected ArtifactRepository localRepository;
97
98 @Parameter( defaultValue = "${project.artifact}", readonly = true )
99 protected Artifact projectArtifact;
100
101
102
103
104
105 @Component
106 protected ArtifactRepositoryFactory artifactRepositoryFactory;
107
108
109
110
111
112
113
114
115
116 protected PlexusContainer container;
117
118
119
120
121
122
123
124
125 public void contextualize( Context context )
126 throws ContextException
127 {
128 container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
129 }
130
131
132
133
134
135 protected ArtifactRepositoryLayout getArtifactRepositoryLayout()
136 throws MojoFailureException
137 {
138 try
139 {
140 ArtifactRepositoryLayout artifactRepositoryLayout = null;
141 artifactRepositoryLayout =
142 (ArtifactRepositoryLayout) container.lookup( "org.apache.maven.artifact."
143 + "repository.layout.ArtifactRepositoryLayout", repositoryLayout );
144 if ( artifactRepositoryLayout == null )
145 {
146 throw new MojoFailureException( "Unknown repository layout '" + repositoryLayout + "'." );
147 }
148 return artifactRepositoryLayout;
149 }
150 catch ( ComponentLookupException e )
151 {
152 throw new MojoFailureException( "Unable to lookup the repository layout component '" + repositoryLayout
153 + "': " + e.getMessage() );
154 }
155 }
156
157
158
159
160
161
162
163
164
165 protected void installArtifact( Artifact artifact, ArtifactRepository artifactRepository,
166 boolean useTimestampInSnapshotFileName )
167 throws MojoExecutionException
168 {
169 if ( artifact != null && artifact.getFile() != null )
170 {
171 try
172 {
173
174
175 artifact.isSnapshot();
176
177 File source = artifact.getFile();
178
179 String localPath = artifactRepository.pathOf( artifact );
180
181 File destination = new File( artifactRepository.getBasedir(), localPath );
182 if ( !destination.getParentFile().exists() )
183 {
184 destination.getParentFile().mkdirs();
185 }
186
187 if ( artifact.isSnapshot() )
188 {
189 if ( !useTimestampInSnapshotFileName )
190 {
191
192 destination = new File( destination.getParentFile(), source.getName() );
193 }
194 }
195
196 if ( !source.isDirectory() )
197 {
198 if ( outputFileNameMapping != null )
199 {
200 String fileName = MappingUtils.evaluateFileNameMapping( outputFileNameMapping, artifact );
201 destination = new File( destination.getParent(), fileName );
202 }
203
204
205 FileUtils.copyFile( source, destination );
206 }
207
208 getLog().info( "Installing artifact " + source.getPath() + " to " + destination );
209
210 }
211 catch ( IOException e )
212 {
213 throw new MojoExecutionException( "Failed to copy artifact.", e );
214 }
215 catch ( InterpolationException e )
216 {
217 throw new MojoExecutionException( "Failed to map file name.", e );
218 }
219 }
220 }
221
222
223
224
225
226
227
228
229 protected void installArtifact( Artifact artifact, ArtifactRepository artifactRepository )
230 throws MojoExecutionException
231 {
232 installArtifact( artifact, artifactRepository, true );
233 }
234
235 }