001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.geronimo.mavenplugins.car;
019:
020: import java.io.File;
021: import java.util.HashMap;
022: import java.util.HashSet;
023: import java.util.List;
024: import java.util.Set;
025:
026: import org.apache.geronimo.kernel.Kernel;
027: import org.apache.geronimo.kernel.basic.BasicKernel;
028: import org.apache.geronimo.kernel.repository.Artifact;
029: import org.apache.geronimo.system.configuration.RepositoryConfigurationStore;
030: import org.apache.geronimo.system.plugin.DownloadResults;
031: import org.apache.geronimo.system.plugin.PluginInstallerGBean;
032: import org.apache.geronimo.system.plugin.SourceRepository;
033: import org.apache.geronimo.system.plugin.model.ArtifactType;
034: import org.apache.geronimo.system.plugin.model.PluginArtifactType;
035: import org.apache.geronimo.system.plugin.model.PluginListType;
036: import org.apache.geronimo.system.plugin.model.PluginType;
037: import org.apache.geronimo.system.plugin.model.AttributesType;
038: import org.apache.geronimo.system.resolver.AliasedArtifactResolver;
039: import org.apache.maven.artifact.repository.ArtifactRepository;
040: import org.codehaus.mojo.pluginsupport.dependency.DependencyTree;
041:
042: /**
043: * Installs Geronimo module CAR files into a target repository to support assembly.
044: *
045: * @version $Rev: 620730 $ $Date: 2008-02-12 01:21:38 -0800 (Tue, 12 Feb 2008) $
046: * @goal install-modules
047: */
048: public class InstallModulesMojo extends AbstractCarMojo {
049: /**
050: * The location of the server repository.
051: *
052: * @parameter expression="${project.build.directory}/assembly"
053: * @required
054: */
055: private File targetServerDirectory = null;
056:
057: /**
058: * The location of the target repository.
059: *
060: * @parameter expression="repository"
061: * @required
062: */
063: private String targetRepositoryPath = null;
064:
065: /**
066: * The location of the target config files.
067: *
068: * @parameter expression="var/config"
069: * @required
070: */
071: private String targetConfigPath = null;
072:
073: /**
074: * ServerInstance specific in plugin configuration, to specify where config.xml and properties updates go.
075: * @parameter
076: */
077: private List<ServerInstance> servers;
078:
079: /**
080: * Configuration to be installed specified as groupId/artifactId/version/type
081: * if none specified, plugin will install all dependencies of type "car"
082: *
083: * @parameter
084: * @optional
085: */
086: private String artifact = null;
087:
088: /**
089: * Location of the source repository for the dependencies
090: *
091: * @parameter expression="${localRepository}"
092: * @required
093: */
094: private ArtifactRepository sourceRepository = null;
095:
096: /**
097: * The location where the properties mapping will be generated.
098: *
099: * @parameter expression="${project.build.directory}/explicit-versions.properties"
100: * @required
101: */
102: private File explicitResolutionProperties = null;
103:
104: /**
105: * The Geronimo repository artifact resolver.
106: * <p/>
107: * <p/>
108: * Using a custom name here to prevent problems that happen when Plexus
109: * injects the Maven resolver into the base-class.
110: * </p>
111: */
112: private AliasedArtifactResolver geronimoArtifactResolver;
113:
114: private RepositoryConfigurationStore sourceStore;
115:
116: /**
117: * @parameter expression="${project.build.directory}/classes/var/config/overrides"
118: * @required
119: */
120: private File overridesDir;
121:
122: /**
123: * @parameter
124: */
125: private List<Override> overrides;
126:
127: /**
128: * Set of artifacts which have already been installed, so we can skip any processing.
129: */
130: private Set installedArtifacts = new HashSet();
131:
132: protected void doExecute() throws Exception {
133: DependencyTree dependencies = dependencyHelper
134: .getDependencies(project);
135: Maven2RepositoryAdapter.ArtifactLookup lookup = new ArtifactLookupImpl(
136: new HashMap<Artifact, org.apache.maven.artifact.Artifact>());
137: SourceRepository sourceRepo = new Maven2RepositoryAdapter(
138: dependencies, lookup);
139: PluginListType pluginList = new PluginListType();
140: String localRepo = sourceRepository.getUrl();
141: if ("file".equals(sourceRepository.getProtocol())) {
142: File localRepoDir = new File(sourceRepository.getBasedir());
143: localRepo = localRepoDir.toURI().toString();
144: }
145: pluginList.getDefaultRepository().add(localRepo);
146: for (org.apache.maven.model.Repository repository : (List<org.apache.maven.model.Repository>) project
147: .getRepositories()) {
148: pluginList.getDefaultRepository().add(repository.getUrl());
149: }
150:
151: if (artifact != null) {
152: pluginList.getPlugin().add(
153: toPluginType(Artifact.create(artifact)));
154: } else {
155: addDependencies(pluginList);
156: }
157: DownloadResults downloadPoller = new DownloadResults();
158: String targetServerPath = targetServerDirectory
159: .getAbsolutePath();
160:
161: Kernel kernel = new BasicKernel("Assembly");
162: try {
163: PluginInstallerGBean installer = new PluginInstallerGBean(
164: targetRepositoryPath, targetServerPath, servers,
165: kernel, getClass().getClassLoader());
166: installer.install(pluginList, sourceRepo, true, null, null,
167: downloadPoller);
168: if (overrides != null) {
169: for (Override override : this .overrides) {
170: AttributesType attributes = override
171: .getOverrides(overridesDir);
172: installer.mergeOverrides(override.getServer(),
173: attributes);
174: }
175: }
176: } finally {
177: kernel.shutdown();
178: }
179: log.info("Installed plugins: ");
180: for (Artifact artifact : downloadPoller.getInstalledConfigIDs()) {
181: log.info(" " + artifact);
182: }
183: log.info("Installed dependencies: ");
184: for (Artifact artifact : downloadPoller
185: .getDependenciesInstalled()) {
186: log.info(" " + artifact);
187: }
188: if (downloadPoller.isFailed()) {
189: throw downloadPoller.getFailure();
190: }
191: }
192:
193: private PluginType toPluginType(Artifact artifact) {
194: PluginType plugin = new PluginType();
195: PluginArtifactType instance = new PluginArtifactType();
196: ArtifactType artifactType = PluginInstallerGBean
197: .toArtifactType(artifact);
198: instance.setModuleId(artifactType);
199: plugin.getPluginArtifact().add(instance);
200: return plugin;
201: }
202:
203: /**
204: * Retrieves all artifact dependencies.
205: *
206: * @param pluginList PluginListType to add dependencies to as PluginType instances.
207: */
208: protected void addDependencies(PluginListType pluginList) {
209:
210: org.apache.maven.artifact.Artifact artifact = project
211: .getArtifact();
212: if (artifact != null
213: && ("car".equals(artifact.getType()) || "jar"
214: .equals(artifact.getType()))
215: && artifact.getFile() != null) {
216: pluginList.getPlugin().add(
217: toPluginType(mavenToGeronimoArtifact(artifact)));
218: }
219:
220: List<org.apache.maven.model.Dependency> projectArtifacts = project
221: .getModel().getDependencies();
222: if (projectArtifacts != null) {
223: for (org.apache.maven.model.Dependency dependency : projectArtifacts) {
224: if (dependency.getScope() == null
225: || "compile".equals(dependency.getScope())) {
226: pluginList
227: .getPlugin()
228: .add(
229: toPluginType(mavenToGeronimoArtifact(dependency)));
230: }
231: }
232: }
233:
234: }
235:
236: }
|