1 package org.apache.maven.plugin.coreit;
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.Artifact;
23 import org.apache.maven.artifact.handler.ArtifactHandler;
24 import org.apache.maven.artifact.installer.ArtifactInstallationException;
25 import org.apache.maven.artifact.installer.ArtifactInstaller;
26 import org.apache.maven.artifact.metadata.ArtifactMetadata;
27 import org.apache.maven.artifact.repository.ArtifactRepository;
28 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
29 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
30 import org.apache.maven.plugin.AbstractMojo;
31 import org.apache.maven.plugin.MojoExecutionException;
32 import org.apache.maven.plugin.MojoFailureException;
33
34 import java.io.File;
35 import java.util.List;
36
37
38
39
40
41
42
43 public class InstallArtifactsMojo
44 extends AbstractMojo
45 {
46
47
48
49
50
51 private List artifacts;
52
53
54
55
56 private ArtifactInstaller artifactInstaller;
57
58
59
60
61 private ArtifactRepositoryFactory artifactRepositoryFactory;
62
63
64
65
66
67
68
69
70 private File assembleDirectory;
71
72
73
74
75
76
77 private String repositoryName;
78
79 public void execute()
80 throws MojoExecutionException, MojoFailureException
81 {
82
83 ArtifactRepositoryLayout artifactRepositoryLayout = new FlatRepositoryLayout();
84
85
86 ArtifactRepository artifactRepository =
87 artifactRepositoryFactory.createDeploymentArtifactRepository( "appassembler", "file://"
88 + assembleDirectory.getAbsolutePath() + "/" + repositoryName, artifactRepositoryLayout, false );
89 for ( Object artifact1 : artifacts )
90 {
91 Artifact artifact = (Artifact) artifact1;
92
93 installArtifact( artifactRepository, artifact );
94 }
95 }
96
97 private void installArtifact( ArtifactRepository artifactRepository, Artifact artifact )
98 throws MojoExecutionException
99 {
100 try
101 {
102
103 artifact.isSnapshot();
104
105 if ( artifact.getFile() != null )
106 {
107 artifactInstaller.install( artifact.getFile(), artifact, artifactRepository );
108 }
109 }
110 catch ( ArtifactInstallationException e )
111 {
112 throw new MojoExecutionException( "Failed to copy artifact.", e );
113 }
114 }
115
116
117
118
119 public static class FlatRepositoryLayout
120 implements ArtifactRepositoryLayout
121 {
122 private static final char ARTIFACT_SEPARATOR = '-';
123
124 private static final char GROUP_SEPARATOR = '.';
125
126 public String pathOf( Artifact artifact )
127 {
128 ArtifactHandler artifactHandler = artifact.getArtifactHandler();
129
130 StringBuilder path = new StringBuilder();
131
132 path.append( artifact.getArtifactId() ).append( ARTIFACT_SEPARATOR ).append( artifact.getVersion() );
133
134 if ( artifact.hasClassifier() )
135 {
136 path.append( ARTIFACT_SEPARATOR ).append( artifact.getClassifier() );
137 }
138
139 if ( artifactHandler.getExtension() != null && artifactHandler.getExtension().length() > 0 )
140 {
141 path.append( GROUP_SEPARATOR ).append( artifactHandler.getExtension() );
142 }
143
144 return path.toString();
145 }
146
147 public String pathOfLocalRepositoryMetadata( ArtifactMetadata metadata, ArtifactRepository repository )
148 {
149 return pathOfRepositoryMetadata( metadata.getLocalFilename( repository ) );
150 }
151
152 private String pathOfRepositoryMetadata( String filename )
153 {
154 StringBuilder path = new StringBuilder();
155
156 path.append( filename );
157
158 return path.toString();
159 }
160
161 public String pathOfRemoteRepositoryMetadata( ArtifactMetadata metadata )
162 {
163 return pathOfRepositoryMetadata( metadata.getRemoteFilename() );
164 }
165 }
166
167 }