View Javadoc

1   package org.apache.maven.plugins.mavenone;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.installer.ArtifactInstallationException;
24  import org.apache.maven.artifact.installer.ArtifactInstaller;
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
27  import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
28  import org.apache.maven.plugin.AbstractMojo;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.codehaus.plexus.util.IOUtil;
31  
32  import java.io.File;
33  import java.io.FileInputStream;
34  import java.io.FileNotFoundException;
35  import java.io.IOException;
36  import java.util.Iterator;
37  import java.util.List;
38  import java.util.Properties;
39  
40  /**
41   * Install the artifact in the Maven 1 local repository.
42   *
43   * @goal install-maven-one-repository
44   * @phase install
45   */
46  public class MavenOneRepositoryInstallMojo
47      extends AbstractMojo
48  {
49      /**
50       * @parameter expression="${project.packaging}"
51       * @required
52       * @readonly
53       */
54      protected String packaging;
55  
56      /**
57       * @parameter expression="${project.file}"
58       * @required
59       * @readonly
60       */
61      private File pomFile;
62  
63      /**
64       * @parameter expression="${project.artifact}"
65       * @required
66       * @readonly
67       */
68      private Artifact artifact;
69  
70      /**
71       * @component
72       * @todo Write Javadoc for this
73       */
74      protected ArtifactInstaller installer;
75  
76      /**
77       * @component
78       * @todo Write Javadoc for this
79       */
80      protected ArtifactRepositoryFactory factory;
81  
82      /**
83       * The location of the local repository.
84       *
85       * @parameter expression="${mavenOneRepository}"
86       */
87      protected String mavenOneRepository;
88  
89      /**
90       * Whether the local repository uses a legacy layout or not.
91       *
92       * @component roleHint="legacy"
93       */
94      private ArtifactRepositoryLayout legacyLayout;
95  
96      /**
97       * @parameter expression="${project.attachedArtifacts}
98       * @required
99       * @readonly
100      */
101     private List attachedArtifacts;
102 
103     public void execute()
104         throws MojoExecutionException
105     {
106         try
107         {
108             if ( mavenOneRepository == null )
109             {
110                 File f = new File( System.getProperty( "user.home" ), "build.properties" );
111                 if ( f.exists() )
112                 {
113                     Properties p = new Properties();
114                     FileInputStream inStream = new FileInputStream( f );
115                     try
116                     {
117                         p.load( inStream );
118                     }
119                     finally
120                     {
121                         IOUtil.close( inStream );
122                     }
123                     mavenOneRepository = p.getProperty( "maven.repo.local" );
124                 }
125 
126                 if ( mavenOneRepository == null )
127                 {
128                     mavenOneRepository = System.getProperty( "user.home" ) + "/.maven/repository";
129                 }
130             }
131 
132             File f = new File( mavenOneRepository );
133             if ( !f.exists() )
134             {
135                 f.mkdirs();
136             }
137 
138             ArtifactRepository localRepository = factory.createDeploymentArtifactRepository( "mavenOneRepository",
139                                                                                              f.toURL().toString(),
140                                                                                              legacyLayout, false );
141 
142             boolean isPomArtifact = "pom".equals( packaging );
143 
144             if ( isPomArtifact )
145             {
146                 installer.install( pomFile, artifact, localRepository );
147             }
148             else
149             {
150                 File file = artifact.getFile();
151                 if ( file == null )
152                 {
153                     getLog().warn( "The packaging for this project did not assign a file to the build artifact" );
154                 }
155                 else
156                 {
157                     installer.install( file, artifact, localRepository );
158                 }
159             }
160 
161             if ( attachedArtifacts != null && !attachedArtifacts.isEmpty() )
162             {
163                 for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
164                 {
165                     Artifact attached = (Artifact) i.next();
166 
167                     installer.install( attached.getFile(), attached, localRepository );
168                 }
169             }            
170 
171         }
172         catch ( ArtifactInstallationException e )
173         {
174             throw new MojoExecutionException( e.getMessage(), e );
175         }
176         catch ( FileNotFoundException e )
177         {
178             throw new MojoExecutionException( e.getMessage(), e );
179         }
180         catch ( IOException e )
181         {
182             throw new MojoExecutionException( e.getMessage(), e );
183         }
184     }
185 }