View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.geronimo.plugin.car;
21  
22  import org.apache.geronimo.kernel.repository.WriteableRepository;
23  import org.apache.geronimo.system.repository.Maven2Repository;
24  
25  import org.apache.geronimo.genesis.ArtifactItem;
26  
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.artifact.Artifact;
29  
30  import java.io.File;
31  
32  /**
33   * Installs one or more artifacts into a local Geronimo repository.
34   *
35   * @goal install-artifacts
36   *
37   * @version $Rev: 451661 $ $Date: 2006-09-30 13:45:53 -0700 (Sat, 30 Sep 2006) $
38   */
39  public class InstallArtifactsMojo
40      extends AbstractCarMojo
41  {
42      /**
43       * The location of the target repository to install artifacts into.
44       *
45       * @parameter
46       * @required
47       */
48      private File repositoryDirectory = null;
49  
50      /**
51       * A array {@link ArtifactItem} instances to be installed into the repository.
52       *
53       * @parameter
54       * @required
55       */
56      private ArtifactItem[] artifacts = null;
57  
58      /**
59       * Flag to indicate that if an artifact exists already, that we should delete it and re-install.
60       *
61       * @parameter default-value="true"
62       */
63      private boolean force = true;
64  
65      protected void doExecute() throws Exception {
66          if (!repositoryDirectory.exists()) {
67              repositoryDirectory.mkdirs();
68              log.info("Created directory: " + repositoryDirectory);
69          }
70          else if (!repositoryDirectory.isDirectory()) {
71              throw new MojoExecutionException("Invalid reposiory directory: " + repositoryDirectory);
72          }
73  
74          WriteableRepository repository = new Maven2Repository(repositoryDirectory);
75  
76          // Install all of the artifacts we were asked to...
77          for (int i=0; i<artifacts.length; i++) {
78              Artifact artifact = getArtifact(artifacts[i]);
79              log.info("Installing: " + artifact);
80  
81              org.apache.geronimo.kernel.repository.Artifact gartifact = mavenArtifactToGeronimo(artifact);
82              if (repository.contains(gartifact)) {
83                  if (force) {
84                      File file = repository.getLocation(gartifact);
85                      log.debug("Force deletion of: " + file);
86  
87                      if (!file.delete()) {
88                          throw new MojoExecutionException("Failed to delete artifact from repository: " + artifacts[i]);
89                      }
90                  }
91                  else {
92                      throw new MojoExecutionException("Artifact already exists in repository: " + artifacts[i]);
93                  }
94              }
95  
96              repository.copyToRepository(artifact.getFile(), gartifact, null);
97          }
98      }
99  
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 }