View Javadoc
1   package org.apache.maven.plugin.ant;
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 java.io.IOException;
23  import java.util.List;
24  import java.util.Properties;
25  
26  import org.apache.maven.artifact.factory.ArtifactFactory;
27  import org.apache.maven.artifact.repository.ArtifactRepository;
28  import org.apache.maven.artifact.resolver.ArtifactResolver;
29  import org.apache.maven.execution.MavenSession;
30  import org.apache.maven.plugin.AbstractMojo;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugins.annotations.Component;
33  import org.apache.maven.plugins.annotations.Mojo;
34  import org.apache.maven.plugins.annotations.Parameter;
35  import org.apache.maven.plugins.annotations.ResolutionScope;
36  import org.apache.maven.project.MavenProject;
37  import org.apache.maven.settings.Settings;
38  
39  /**
40   * Generate Ant build files.
41   *
42   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
43   * @version $Id: AntMojo.java 1640228 2014-11-17 21:20:42Z hboutemy $
44   * @todo change this to use the artifact ant tasks instead of :get
45   */
46  @Mojo( name = "ant", requiresDependencyResolution = ResolutionScope.TEST )
47  public class AntMojo
48      extends AbstractMojo
49  {
50      // ----------------------------------------------------------------------
51      // Mojo components
52      // ----------------------------------------------------------------------
53  
54      /**
55       * Used for resolving artifacts.
56       */
57      @Component
58      private ArtifactResolver resolver;
59  
60      /**
61       * Factory for creating artifact objects.
62       */
63      @Component
64      private ArtifactFactory factory;
65  
66      // ----------------------------------------------------------------------
67      // Mojo parameters
68      // ----------------------------------------------------------------------
69  
70      /**
71       * The project to create a build for.
72       */
73      @Parameter( defaultValue = "${project}", readonly = true, required = true )
74      private MavenProject project;
75  
76      /**
77       * The local repository where the artifacts are located.
78       */
79      @Parameter( defaultValue = "${localRepository}", required = true, readonly = true )
80      private ArtifactRepository localRepository;
81  
82      /**
83       * The remote repositories where artifacts are located.
84       */
85      @Parameter( defaultValue = "${project.remoteArtifactRepositories}", readonly = true )
86      private List remoteRepositories;
87  
88      /**
89       * The current user system settings for use in Maven.
90       */
91      @Parameter( defaultValue = "${settings}", readonly = true, required = true )
92      private Settings settings;
93  
94      /**
95       * Whether or not to overwrite the <code>build.xml</code> file.
96       */
97      @Parameter( property = "overwrite", defaultValue = "false" )
98      private boolean overwrite;
99  
100     /**
101      * The current Maven session.
102      */
103     @Parameter( defaultValue = "${session}", readonly = true, required = true )
104     private MavenSession session;
105 
106     /**
107      * {@inheritDoc}
108      */
109     public void execute()
110         throws MojoExecutionException
111     {
112         ArtifactResolverWrapper artifactResolverWrapper =
113             ArtifactResolverWrapper.getInstance( resolver, factory, localRepository, remoteRepositories );
114 
115         Properties executionProperties = ( session != null ) ? session.getExecutionProperties() : null;
116 
117         AntBuildWriter antBuildWriter =
118             new AntBuildWriter( project, artifactResolverWrapper, settings, overwrite, executionProperties );
119 
120         try
121         {
122             antBuildWriter.writeBuildXmls();
123             antBuildWriter.writeBuildProperties();
124         }
125         catch ( IOException e )
126         {
127             throw new MojoExecutionException( "Error building Ant script: " + e.getMessage(), e );
128         }
129 
130         getLog().info(
131             "Wrote Ant project for " + project.getArtifactId() + " to " + project.getBasedir().getAbsolutePath() );
132     }
133 }