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 org.apache.geronimo.kernel.repository.WriteableRepository;
023    import org.apache.geronimo.system.repository.Maven2Repository;
024    
025    import org.codehaus.mojo.pluginsupport.util.ArtifactItem;
026    
027    import org.apache.maven.plugin.MojoExecutionException;
028    import org.apache.maven.artifact.Artifact;
029    
030    import java.io.File;
031    
032    /**
033     * Installs one or more artifacts into a local Geronimo repository.
034     *
035     * @goal install-artifacts
036     *
037     * @version $Rev: 524717 $ $Date: 2007-04-01 23:39:19 -0400 (Sun, 01 Apr 2007) $
038     */
039    public class InstallArtifactsMojo
040        extends AbstractCarMojo
041    {
042        /**
043         * The location of the target repository to install artifacts into.
044         *
045         * @parameter
046         * @required
047         */
048        private File repositoryDirectory = null;
049    
050        /**
051         * A array {@link ArtifactItem} instances to be installed into the repository.
052         *
053         * @parameter
054         * @required
055         */
056        private ArtifactItem[] artifacts = null;
057    
058        /**
059         * Flag to indicate that if an artifact exists already, that we should delete it and re-install.
060         *
061         * @parameter default-value="true"
062         */
063        private boolean force = true;
064    
065        protected void doExecute() throws Exception {
066            if (!repositoryDirectory.exists()) {
067                repositoryDirectory.mkdirs();
068                log.info("Created directory: " + repositoryDirectory);
069            }
070            else if (!repositoryDirectory.isDirectory()) {
071                throw new MojoExecutionException("Invalid reposiory directory: " + repositoryDirectory);
072            }
073    
074            WriteableRepository repository = new Maven2Repository(repositoryDirectory);
075    
076            // Install all of the artifacts we were asked to...
077            for (int i=0; i<artifacts.length; i++) {
078                Artifact artifact = getArtifact(artifacts[i]);
079                log.info("Installing: " + artifact);
080    
081                org.apache.geronimo.kernel.repository.Artifact gartifact = mavenArtifactToGeronimo(artifact);
082                if (repository.contains(gartifact)) {
083                    if (force) {
084                        File file = repository.getLocation(gartifact);
085                        log.debug("Force deletion of: " + file);
086    
087                        if (!file.delete()) {
088                            throw new MojoExecutionException("Failed to delete artifact from repository: " + artifacts[i]);
089                        }
090                    }
091                    else {
092                        throw new MojoExecutionException("Artifact already exists in repository: " + artifacts[i]);
093                    }
094                }
095    
096                repository.copyToRepository(artifact.getFile(), gartifact, null);
097            }
098        }
099    
100        /**
101         * Convert a Maven artifact into a the Geronimo flavor.
102         */
103        private org.apache.geronimo.kernel.repository.Artifact mavenArtifactToGeronimo(final Artifact artifact) {
104            assert artifact != null;
105    
106            return new org.apache.geronimo.kernel.repository.Artifact(
107                artifact.getGroupId(),
108                artifact.getArtifactId(),
109                artifact.getVersion(),
110                artifact.getType()
111            );
112        }
113    }