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 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 if ( mavenOneRepository == null )
109 {
110 File f = new File( System.getProperty( "user.home" ), "build.properties" );
111 if ( f.exists() )
112 {
113 Properties p = new Properties();
114 FileInputStream inStream = new FileInputStream( f );
115 try
116 {
117 p.load( inStream );
118 }
119 finally
120 {
121 IOUtil.close( inStream );
122 }
123 mavenOneRepository = p.getProperty( "maven.repo.local" );
124 }
125
126 if ( mavenOneRepository == null )
127 {
128 mavenOneRepository = System.getProperty( "user.home" ) + "/.maven/repository";
129 }
130 }
131
132 File f = new File( mavenOneRepository );
133 if ( !f.exists() )
134 {
135 f.mkdirs();
136 }
137
138 ArtifactRepository localRepository = factory.createDeploymentArtifactRepository( "mavenOneRepository",
139 f.toURL().toString(),
140 legacyLayout, false );
141
142 boolean isPomArtifact = "pom".equals( packaging );
143
144 if ( isPomArtifact )
145 {
146 installer.install( pomFile, artifact, localRepository );
147 }
148 else
149 {
150 File file = artifact.getFile();
151 if ( file == null )
152 {
153 getLog().warn( "The packaging for this project did not assign a file to the build artifact" );
154 }
155 else
156 {
157 installer.install( file, artifact, localRepository );
158 }
159 }
160
161 if ( attachedArtifacts != null && !attachedArtifacts.isEmpty() )
162 {
163 for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
164 {
165 Artifact attached = (Artifact) i.next();
166
167 installer.install( attached.getFile(), attached, localRepository );
168 }
169 }
170
171 }
172 catch ( ArtifactInstallationException e )
173 {
174 throw new MojoExecutionException( e.getMessage(), e );
175 }
176 catch ( FileNotFoundException e )
177 {
178 throw new MojoExecutionException( e.getMessage(), e );
179 }
180 catch ( IOException e )
181 {
182 throw new MojoExecutionException( e.getMessage(), e );
183 }
184 }
185 }