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     */
019    
020    package org.apache.geronimo.mavenplugins.car;
021    
022    import java.io.File;
023    import java.util.HashMap;
024    import java.util.HashSet;
025    import java.util.List;
026    import java.util.Set;
027    
028    import org.apache.geronimo.kernel.Kernel;
029    import org.apache.geronimo.kernel.basic.BasicKernel;
030    import org.apache.geronimo.kernel.repository.Artifact;
031    import org.apache.geronimo.system.configuration.RepositoryConfigurationStore;
032    import org.apache.geronimo.system.plugin.DownloadResults;
033    import org.apache.geronimo.system.plugin.PluginInstallerGBean;
034    import org.apache.geronimo.system.plugin.SourceRepository;
035    import org.apache.geronimo.system.plugin.model.ArtifactType;
036    import org.apache.geronimo.system.plugin.model.PluginArtifactType;
037    import org.apache.geronimo.system.plugin.model.PluginListType;
038    import org.apache.geronimo.system.plugin.model.PluginType;
039    import org.apache.geronimo.system.plugin.model.AttributesType;
040    import org.apache.geronimo.system.resolver.AliasedArtifactResolver;
041    import org.apache.maven.artifact.repository.ArtifactRepository;
042    import org.codehaus.mojo.pluginsupport.dependency.DependencyTree;
043    
044    /**
045     * Installs Geronimo module CAR files into a target repository to support assembly.
046     *
047     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
048     * @goal install-modules
049     */
050    public class InstallModulesMojo
051        extends AbstractCarMojo
052    {
053        /**
054         * The location of the server repository.
055         *
056         * @parameter expression="${project.build.directory}/assembly"
057         * @required
058         */
059        private File targetServerDirectory = null;
060    
061        /**
062         * The location of the target repository.
063         *
064         * @parameter expression="repository"
065         * @required
066         */
067        private String targetRepositoryPath = null;
068    
069        /**
070         * The location of the target config files.
071         *
072         * @parameter expression="var/config"
073         * @required
074         */
075        private String targetConfigPath = null;
076    
077        /**
078         * ServerInstance specific in plugin configuration, to specify where config.xml and properties updates go.
079         * @parameter
080         */
081        private List<ServerInstance> servers;
082    
083        /**
084         * Configuration to be installed specified as groupId/artifactId/version/type
085         * if none specified, plugin will install all dependencies of type "car"
086         *
087         * @parameter
088         * @optional
089         */
090        private String artifact = null;
091    
092        /**
093         * Location of the source repository for the dependencies
094         *
095         * @parameter expression="${localRepository}"
096         * @required
097         */
098        private ArtifactRepository sourceRepository = null;
099    
100        /**
101         * The location where the properties mapping will be generated.
102         *
103         * @parameter expression="${project.build.directory}/explicit-versions.properties"
104         * @required
105         */
106        private File explicitResolutionProperties = null;
107    
108        /**
109         * The Geronimo repository artifact resolver.
110         * <p/>
111         * <p/>
112         * Using a custom name here to prevent problems that happen when Plexus
113         * injects the Maven resolver into the base-class.
114         * </p>
115         */
116        private AliasedArtifactResolver geronimoArtifactResolver;
117    
118        private RepositoryConfigurationStore sourceStore;
119    
120    
121        /**
122         * @parameter expression="${project.build.directory}/classes/var/config/overrides"
123         * @required
124         */
125        private File overridesDir;
126    
127        /**
128         * @parameter
129         */
130        private List<Override> overrides;
131    
132        /**
133         * Set of artifacts which have already been installed, so we can skip any processing.
134         */
135        private Set installedArtifacts = new HashSet();
136    
137        protected void doExecute() throws Exception {
138            getDependencies(project);
139            Maven2RepositoryAdapter.ArtifactLookup lookup = new ArtifactLookupImpl(new HashMap<Artifact, org.apache.maven.artifact.Artifact>());
140            SourceRepository sourceRepo = new Maven2RepositoryAdapter(dependencies, lookup);
141            PluginListType pluginList = new PluginListType();
142            String localRepo = sourceRepository.getUrl();
143            if ("file".equals(sourceRepository.getProtocol())) {
144                File localRepoDir = new File(sourceRepository.getBasedir());
145                localRepo = localRepoDir.toURI().toString();
146            }
147            pluginList.getDefaultRepository().add(localRepo);
148            for (org.apache.maven.model.Repository repository: (List<org.apache.maven.model.Repository>)project.getRepositories()) {
149                pluginList.getDefaultRepository().add(repository.getUrl());
150            }
151    
152            if (artifact != null) {
153                pluginList.getPlugin().add(toPluginType(Artifact.create(artifact)));
154            } else {
155                addDependencies(pluginList);
156            }
157            DownloadResults downloadPoller = new DownloadResults();
158            String targetServerPath = targetServerDirectory.getAbsolutePath();
159    
160            Kernel kernel = new BasicKernel("Assembly");
161            try {
162                PluginInstallerGBean installer = new PluginInstallerGBean(targetRepositoryPath, targetServerPath, servers, kernel, getClass().getClassLoader());
163                installer.install(pluginList, sourceRepo, true, null, null, downloadPoller);
164                if (overrides != null) {
165                    for (Override override: this.overrides) {
166                        AttributesType attributes = override.getOverrides(overridesDir);
167                        installer.mergeOverrides(override.getServer(), attributes);
168                    }
169                }
170            } finally {
171                kernel.shutdown();
172            }
173            log.info("Installed plugins: ");
174            for (Artifact artifact: downloadPoller.getInstalledConfigIDs()) {
175                log.info("    " + artifact);
176            }
177            log.info("Installed dependencies: ");
178            for (Artifact artifact: downloadPoller.getDependenciesInstalled()) {
179                log.info("    " + artifact);
180            }
181            if (downloadPoller.isFailed()) {
182                throw downloadPoller.getFailure();
183            }
184        }
185    
186    
187        private PluginType toPluginType(Artifact artifact) {
188            PluginType plugin = new PluginType();
189            PluginArtifactType instance = new PluginArtifactType();
190            ArtifactType artifactType = PluginInstallerGBean.toArtifactType(artifact);
191            instance.setModuleId(artifactType);
192            plugin.getPluginArtifact().add(instance);
193            return plugin;
194        }
195    
196        /**
197         * Retrieves all artifact dependencies.
198         *
199         * @param pluginList PluginListType to add dependencies to as PluginType instances.
200         */
201        protected void addDependencies(PluginListType pluginList) {
202    
203            org.apache.maven.artifact.Artifact artifact = project.getArtifact();
204            if (artifact != null && ("car".equals(artifact.getType()) || "jar".equals(artifact.getType())) && artifact.getFile() != null) {
205                pluginList.getPlugin().add(toPluginType(mavenToGeronimoArtifact(artifact)));
206            }
207    
208            List<org.apache.maven.model.Dependency> projectArtifacts = project.getModel().getDependencies();
209            if (projectArtifacts != null) {
210                for (org.apache.maven.model.Dependency dependency: projectArtifacts) {
211                    if (dependency.getScope() == null || "compile".equals(dependency.getScope())) {
212                        pluginList.getPlugin().add(toPluginType(mavenToGeronimoArtifact(dependency)));
213                    }
214                }
215            }
216    
217        }
218    
219    }