1 | |
package org.apache.maven.plugins.mavenone; |
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.installer.ArtifactInstallationException; |
24 | |
import org.apache.maven.artifact.installer.ArtifactInstaller; |
25 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
26 | |
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; |
27 | |
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; |
28 | |
import org.apache.maven.plugin.AbstractMojo; |
29 | |
import org.apache.maven.plugin.MojoExecutionException; |
30 | |
import org.codehaus.plexus.util.IOUtil; |
31 | |
|
32 | |
import java.io.File; |
33 | |
import java.io.FileInputStream; |
34 | |
import java.io.FileNotFoundException; |
35 | |
import java.io.IOException; |
36 | |
import java.util.Iterator; |
37 | |
import java.util.List; |
38 | |
import java.util.Properties; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 0 | public class MavenOneRepositoryInstallMojo |
47 | |
extends AbstractMojo |
48 | |
{ |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
protected String packaging; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
private File pomFile; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
private Artifact artifact; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
protected ArtifactInstaller installer; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
protected ArtifactRepositoryFactory factory; |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
protected String mavenOneRepository; |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
private ArtifactRepositoryLayout legacyLayout; |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
private List attachedArtifacts; |
102 | |
|
103 | |
public void execute() |
104 | |
throws MojoExecutionException |
105 | |
{ |
106 | |
try |
107 | |
{ |
108 | 0 | if ( mavenOneRepository == null ) |
109 | |
{ |
110 | 0 | File f = new File( System.getProperty( "user.home" ), "build.properties" ); |
111 | 0 | if ( f.exists() ) |
112 | |
{ |
113 | 0 | Properties p = new Properties(); |
114 | 0 | FileInputStream inStream = new FileInputStream( f ); |
115 | |
try |
116 | |
{ |
117 | 0 | p.load( inStream ); |
118 | |
} |
119 | |
finally |
120 | |
{ |
121 | 0 | IOUtil.close( inStream ); |
122 | 0 | } |
123 | 0 | mavenOneRepository = p.getProperty( "maven.repo.local" ); |
124 | |
} |
125 | |
|
126 | 0 | if ( mavenOneRepository == null ) |
127 | |
{ |
128 | 0 | mavenOneRepository = System.getProperty( "user.home" ) + "/.maven/repository"; |
129 | |
} |
130 | |
} |
131 | |
|
132 | 0 | File f = new File( mavenOneRepository ); |
133 | 0 | if ( !f.exists() ) |
134 | |
{ |
135 | 0 | f.mkdirs(); |
136 | |
} |
137 | |
|
138 | 0 | ArtifactRepository localRepository = factory.createDeploymentArtifactRepository( "mavenOneRepository", |
139 | |
f.toURL().toString(), |
140 | |
legacyLayout, false ); |
141 | |
|
142 | 0 | boolean isPomArtifact = "pom".equals( packaging ); |
143 | |
|
144 | 0 | if ( isPomArtifact ) |
145 | |
{ |
146 | 0 | installer.install( pomFile, artifact, localRepository ); |
147 | |
} |
148 | |
else |
149 | |
{ |
150 | 0 | File file = artifact.getFile(); |
151 | 0 | if ( file == null ) |
152 | |
{ |
153 | 0 | throw new MojoExecutionException( |
154 | |
"The packaging for this project did not assign a file to the build artifact" ); |
155 | |
} |
156 | 0 | installer.install( file, artifact, localRepository ); |
157 | |
} |
158 | |
|
159 | 0 | if ( attachedArtifacts != null && !attachedArtifacts.isEmpty() ) |
160 | |
{ |
161 | 0 | for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); ) |
162 | |
{ |
163 | 0 | Artifact attached = (Artifact) i.next(); |
164 | |
|
165 | 0 | installer.install( attached.getFile(), attached, localRepository ); |
166 | |
} |
167 | |
} |
168 | |
|
169 | |
} |
170 | 0 | catch ( ArtifactInstallationException e ) |
171 | |
{ |
172 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
173 | |
} |
174 | 0 | catch ( FileNotFoundException e ) |
175 | |
{ |
176 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
177 | |
} |
178 | 0 | catch ( IOException e ) |
179 | |
{ |
180 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
181 | 0 | } |
182 | 0 | } |
183 | |
} |