1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.geronimo.plugin.car;
18
19 import org.apache.geronimo.kernel.config.InvalidConfigException;
20 import org.apache.geronimo.kernel.config.NoSuchConfigException;
21 import org.apache.geronimo.kernel.config.ConfigurationData;
22 import org.apache.geronimo.kernel.repository.Artifact;
23 import org.apache.geronimo.kernel.repository.ArtifactManager;
24 import org.apache.geronimo.kernel.repository.ArtifactResolver;
25 import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
26 import org.apache.geronimo.kernel.repository.Environment;
27 import org.apache.geronimo.kernel.repository.FileWriteMonitor;
28 import org.apache.geronimo.kernel.repository.MissingDependencyException;
29 import org.apache.geronimo.kernel.repository.Dependency;
30 import org.apache.geronimo.kernel.repository.WritableListableRepository;
31 import org.apache.geronimo.system.repository.Maven2Repository;
32 import org.apache.geronimo.system.configuration.RepositoryConfigurationStore;
33 import org.apache.geronimo.system.resolver.ExplicitDefaultArtifactResolver;
34
35 import org.apache.geronimo.plugin.MojoSupport;
36
37 import org.apache.maven.artifact.repository.ArtifactRepository;
38 import org.apache.maven.project.MavenProject;
39 import org.apache.maven.plugin.MojoExecutionException;
40
41 import java.io.File;
42 import java.io.FileInputStream;
43 import java.io.IOException;
44 import java.io.InputStream;
45
46 import java.util.Iterator;
47 import java.util.HashSet;
48 import java.util.LinkedHashSet;
49 import java.util.Set;
50 import java.util.Collections;
51
52 /**
53 * Installs CAR files into a target repository to support assembly.
54 *
55 * @goal installConfig
56 *
57 * @version $Rev: 428888 $ $Date: 2006-08-04 23:21:54 +0200 (ven., 04 août 2006) $
58 */
59 public class InstallConfigMojo
60 extends AbstractCarMojo
61 {
62 public static final FileWriteMonitor LOG_COPY_START = new StartFileWriteMonitor();
63
64 /**
65 * Root file of the TargetRepository.
66 *
67 * @parameter expression="${project.build.directory}"
68 */
69 private String targetRoot;
70
71 /**
72 * The location of the target repository relative to targetRoot.
73 *
74 * @parameter default-value="archive-tmp/repository"
75 * @required
76 */
77 private String targetRepository;
78
79 /**
80 * Configuration to be installed specified as groupId/artifactId/version/type
81 * if none specified, plugin will install all dependencies of type "car"
82 *
83 * @parameter
84 * @optional
85 */
86 private String artifact;
87
88 /**
89 * Location of the source repository for the dependencies
90 *
91 * @parameter expression="${localRepository}"
92 * @required
93 */
94 private ArtifactRepository sourceRepository;
95
96 /**
97 * The location where the properties mapping will be generated.
98 *
99 * @parameter expression="${project.build.directory}/explicit-versions.properties"
100 * @required
101 */
102 private File explicitResolutionProperties;
103
104 private ArtifactResolver artifactResolver;
105
106 private WritableListableRepository targetRepo;
107 private RepositoryConfigurationStore targetStore;
108
109 private WritableListableRepository sourceRepo;
110 private RepositoryConfigurationStore sourceStore;
111
112 protected void doExecute() throws Exception {
113 generateExplicitVersionProperties(explicitResolutionProperties);
114
115 sourceRepo = new Maven2Repository(new File(sourceRepository.getBasedir()));
116 sourceStore = new RepositoryConfigurationStore(sourceRepo);
117
118 File targetRepoFile = new File(targetRoot, targetRepository);
119 if (!targetRepoFile.exists()) {
120 targetRepoFile.mkdirs();
121 }
122
123 targetRepo = new Maven2Repository(targetRepoFile);
124 targetStore = new RepositoryConfigurationStore(targetRepo);
125
126 Artifact configId;
127 ArtifactManager artifactManager = new DefaultArtifactManager();
128 artifactResolver = new ExplicitDefaultArtifactResolver(
129 explicitResolutionProperties.getPath(),
130 artifactManager,
131 Collections.singleton(sourceRepo),
132 null);
133
134 if ( artifact != null ) {
135 configId = Artifact.create(artifact);
136 execute(configId);
137 }
138 else {
139 Iterator itr = getDependencies().iterator();
140 while (itr.hasNext()) {
141 org.apache.maven.artifact.Artifact mavenArtifact = (org.apache.maven.artifact.Artifact) itr.next();
142 if ("car".equals(mavenArtifact.getType())) {
143 configId = new Artifact(mavenArtifact.getGroupId(), mavenArtifact.getArtifactId(), mavenArtifact.getVersion(), "car");
144 execute(configId);
145 }
146 }
147 }
148 }
149
150 /**
151 * Retrieves all artifact dependencies.
152 *
153 * @return A HashSet of artifacts
154 */
155 protected Set getDependencies() {
156 Set dependenciesSet = new HashSet();
157
158 if (project.getArtifact() != null && project.getArtifact().getFile() != null) {
159 dependenciesSet.add( project.getArtifact() );
160 }
161
162 Set projectArtifacts = project.getArtifacts();
163 if (projectArtifacts != null) {
164 dependenciesSet.addAll(projectArtifacts);
165 }
166
167 return dependenciesSet;
168 }
169
170 private void execute(Artifact configId) throws Exception {
171 LinkedHashSet dependencies;
172
173
174 if (!sourceRepo.contains(configId)) {
175 throw new NoSuchConfigException(configId);
176 }
177
178
179 if (targetStore.containsConfiguration(configId)) {
180 log.info("Configuration " + configId + " already present in configuration store");
181 return;
182 }
183
184 if (sourceStore.containsConfiguration(configId)) {
185
186 if (!targetStore.containsConfiguration(configId)) {
187 File sourceFile = sourceRepo.getLocation(configId);
188 InputStream in = new FileInputStream(sourceFile);
189 try {
190 targetStore.install(in, (int)sourceFile.length(), configId, LOG_COPY_START);
191 }
192 finally {
193 in.close();
194 }
195 }
196
197
198 try {
199 ConfigurationData configurationData = targetStore.loadConfiguration(configId);
200 Environment environment = configurationData.getEnvironment();
201 dependencies = new LinkedHashSet();
202 for (Iterator iterator = environment.getDependencies().iterator(); iterator.hasNext();) {
203 Dependency dependency = (Dependency) iterator.next();
204 dependencies.add(dependency.getArtifact());
205 }
206
207 log.info("Installed configuration " + configId);
208 }
209 catch (IOException e) {
210 throw new InvalidConfigException("Unable to load configuration: " + configId, e);
211 }
212 catch (NoSuchConfigException e) {
213 throw new InvalidConfigException("Unable to load configuration: " + configId, e);
214 }
215 }
216 else {
217 if (!sourceRepo.contains(configId)) {
218 throw new RuntimeException("Dependency: " + configId + " not found in local maven repo: for configuration: " + artifact);
219 }
220
221
222 if (!targetRepo.contains(configId)) {
223 File sourceFile = sourceRepo.getLocation(configId);
224 InputStream in = new FileInputStream(sourceFile);
225 try {
226 targetRepo.copyToRepository(in, (int)sourceFile.length(), configId, LOG_COPY_START);
227 }
228 finally {
229 in.close();
230 }
231 }
232
233
234 dependencies = sourceRepo.getDependencies(configId);
235 }
236
237 dependencies = artifactResolver.resolveInClassLoader(dependencies);
238 for (Iterator iterator = dependencies.iterator(); iterator.hasNext();) {
239 Artifact artifact = (Artifact) iterator.next();
240 execute(artifact);
241 }
242 }
243
244 private static class StartFileWriteMonitor implements FileWriteMonitor {
245 public void writeStarted(String fileDescription, int fileSize) {
246
247
248
249
250 System.out.println("Copying " + fileDescription);
251 }
252
253 public void writeProgress(int bytes) {
254
255 }
256
257 public void writeComplete(int bytes) {
258
259 }
260 }
261 }