You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2017/12/20 09:26:44 UTC

[maven-ant-plugin] 26/50: MANT-14: Generate a build-user.xml à la Eclipse

This is an automated email from the ASF dual-hosted git repository.

hboutemy pushed a commit to annotated tag maven-ant-plugin-2.0
in repository https://gitbox.apache.org/repos/asf/maven-ant-plugin.git

commit 3e8b91fc036ffbfb66d5ae7c9ada30e9ffc84166
Author: Vincent Siveton <vs...@apache.org>
AuthorDate: Sun Sep 24 13:47:52 2006 +0000

    MANT-14: Generate a build-user.xml à la Eclipse
    
    o Added an overwrite parameter in the mojo to overwrite the build.xml
    o Updated AntBuildWriter to generate build.xml (could be overwrite or not), maven-build.xml and maven-build.properties
    o Using import task of Ant in build.xml so target could be override
    o Updated test case
    
    git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk/maven-ant-plugin@449404 13f79535-47bb-0310-9956-ffa450edef68
---
 .../apache/maven/plugin/ant/AntBuildWriter.java    | 265 ++++++++++++++-------
 .../java/org/apache/maven/plugin/ant/AntMojo.java  |  21 +-
 .../org/apache/maven/plugin/ant/AntMojoTest.java   |  16 +-
 3 files changed, 208 insertions(+), 94 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java b/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java
index 4821756..f8273d3 100644
--- a/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java
+++ b/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java
@@ -42,7 +42,12 @@ import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 import org.codehaus.plexus.util.xml.XMLWriter;
 
 /**
- * Write an <code>build.xml<code> for <a href="http://ant.apache.org">Ant</a> 1.6.2 or above.
+ * Write Ant build files from <code>Maven Project</code> for <a href="http://ant.apache.org">Ant</a> 1.6.2 or above:
+ * <ul>
+ * <li>build.xml</li>
+ * <li>maven-build.xml</li>
+ * <li>maven-build.properties</li>
+ * </ul>
  *
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
@@ -57,14 +62,18 @@ public class AntBuildWriter
 
     /**
      * The default build file name (build.xml)
-     * @see Main#DEFAULT_BUILD_FILENAME
      */
     protected static final String DEFAULT_BUILD_FILENAME = Main.DEFAULT_BUILD_FILENAME;
 
     /**
+     * The default generated build file name
+     */
+    protected static final String DEFAULT_MAVEN_BUILD_FILENAME = "maven-build.xml";
+
+    /**
      * The default build properties file name
      */
-    protected static final String DEFAULT_PROPERTIES_FILENAME = "build.properties";
+    protected static final String DEFAULT_MAVEN_PROPERTIES_FILENAME = "maven-build.properties";
 
     private MavenProject project;
 
@@ -72,27 +81,129 @@ public class AntBuildWriter
 
     private Settings settings;
 
+    private boolean overwrite;
+
     /**
      * @param project
      * @param localRepository
      * @param settings
+     * @param overwrite
      */
-    public AntBuildWriter( MavenProject project, File localRepository, Settings settings )
+    public AntBuildWriter( MavenProject project, File localRepository, Settings settings, boolean overwrite )
     {
         this.project = project;
         this.localRepository = localRepository;
         this.settings = settings;
+        this.overwrite = overwrite;
     }
 
-    // ----------------------------------------------------------------------
-    // build.xml
-    // ----------------------------------------------------------------------
+    /**
+     * Generate Ant build XML files
+     *
+     * @throws IOException
+     */
+    protected void writeBuildXmls()
+        throws IOException
+    {
+        writeGeneratedBuildXml();
+        writeBuildXml();
+    }
+
+    /**
+     * Generate <code>maven-build.properties</code>
+     *
+     * @see #DEFAULT_MAVEN_PROPERTIES_FILENAME
+     * @throws IOException
+     */
+    protected void writeBuildProperties()
+        throws IOException
+    {
+        FileOutputStream os = new FileOutputStream( new File( project.getBasedir(), DEFAULT_MAVEN_PROPERTIES_FILENAME ) );
+        Properties properties = new Properties();
+
+        // ----------------------------------------------------------------------
+        // Build properties
+        // ----------------------------------------------------------------------
+
+        addProperty( properties, "maven.build.finalName", PathUtils.toRelative( project.getBasedir(), project
+            .getBuild().getFinalName() ) );
+
+        // target
+        addProperty( properties, "maven.build.dir", PathUtils.toRelative( project.getBasedir(), project.getBuild()
+            .getDirectory() ) );
+
+        // ${maven.build.dir}/classes
+        addProperty( properties, "maven.build.outputDir", "${maven.build.dir}/"
+            + PathUtils.toRelative( new File( project.getBasedir(), properties.getProperty( "maven.build.dir" ) ),
+                                    project.getBuild().getOutputDirectory() ) );
+        // src/main/java
+        if ( !project.getCompileSourceRoots().isEmpty() )
+        {
+            String[] compileSourceRoots = (String[]) project.getCompileSourceRoots().toArray( new String[0] );
+            for ( int i = 0; i < compileSourceRoots.length; i++ )
+            {
+                addProperty( properties, "maven.build.srcDir." + i, PathUtils.toRelative( project.getBasedir(),
+                                                                                          compileSourceRoots[i] ) );
+            }
+        }
+        // src/main/resources
+        if ( project.getBuild().getResources() != null )
+        {
+            Resource[] array = (Resource[]) project.getBuild().getResources().toArray( new Resource[0] );
+            for ( int i = 0; i < array.length; i++ )
+            {
+                addProperty( properties, "maven.build.resourceDir." + i, PathUtils.toRelative( project.getBasedir(),
+                                                                                               array[i].getDirectory() ) );
+            }
+        }
 
-    protected void writeBuildXml()
+        // ${maven.build.dir}/test-classes
+        addProperty( properties, "maven.build.testOutputDir", "${maven.build.dir}/"
+            + PathUtils.toRelative( new File( project.getBasedir(), properties.getProperty( "maven.build.dir" ) ),
+                                    project.getBuild().getTestOutputDirectory() ) );
+        // src/test/java
+        if ( !project.getTestCompileSourceRoots().isEmpty() )
+        {
+            String[] compileSourceRoots = (String[]) project.getTestCompileSourceRoots().toArray( new String[0] );
+            for ( int i = 0; i < compileSourceRoots.length; i++ )
+            {
+                addProperty( properties, "maven.build.testDir." + i, PathUtils.toRelative( project.getBasedir(),
+                                                                                           compileSourceRoots[i] ) );
+            }
+        }
+        // src/test/resources
+        if ( project.getBuild().getTestResources() != null )
+        {
+            Resource[] array = (Resource[]) project.getBuild().getTestResources().toArray( new Resource[0] );
+            for ( int i = 0; i < array.length; i++ )
+            {
+                addProperty( properties, "maven.build.testResourceDir." + i, PathUtils
+                    .toRelative( project.getBasedir(), array[i].getDirectory() ) );
+            }
+        }
+
+        // ----------------------------------------------------------------------
+        // Settings properties
+        // ----------------------------------------------------------------------
+
+        addProperty( properties, "maven.settings.offline", String.valueOf( settings.isOffline() ) );
+        addProperty( properties, "maven.settings.interactiveMode", String.valueOf( settings.isInteractiveMode() ) );
+        addProperty( properties, "maven.repo.local", localRepository.getAbsolutePath() );
+
+        properties.store( os, "Generated by Maven Ant Plugin - DO NOT EDIT THIS FILE!" );
+    }
+
+    /**
+     * Generate an <code>maven-build.xml</code>
+     *
+     * @see #DEFAULT_MAVEN_BUILD_FILENAME
+     * @throws IOException
+     */
+    private void writeGeneratedBuildXml()
         throws IOException
     {
         // TODO: parameter
-        FileWriter w = new FileWriter( new File( project.getBasedir(), DEFAULT_BUILD_FILENAME ) );
+        FileWriter w = new FileWriter( new File( project.getBasedir(), DEFAULT_MAVEN_BUILD_FILENAME ) );
 
         XMLWriter writer = new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", DEFAULT_INDENTATION_SIZE ), "UTF-8",
                                                      null );
@@ -168,82 +279,67 @@ public class AntBuildWriter
         IOUtil.close( w );
     }
 
-    protected void writeBuildProperties()
+    /**
+     * Generate an generic <code>build.xml</code> if not already exist
+     *
+     * @see #DEFAULT_BUILD_FILENAME
+     * @throws IOException
+     */
+    private void writeBuildXml()
         throws IOException
     {
-        FileOutputStream os = new FileOutputStream( new File( project.getBasedir(), DEFAULT_PROPERTIES_FILENAME ) );
-        Properties properties = new Properties();
+        if ( new File( project.getBasedir(), DEFAULT_BUILD_FILENAME ).exists() && !overwrite )
+        {
+            return;
+        }
+
+        FileWriter w = new FileWriter( new File( project.getBasedir(), DEFAULT_BUILD_FILENAME ) );
+
+        XMLWriter writer = new PrettyPrintXMLWriter( w, StringUtils.repeat( " ", DEFAULT_INDENTATION_SIZE ), "UTF-8",
+                                                     null );
 
         // ----------------------------------------------------------------------
-        // Build properties
+        // <!-- comments -->
         // ----------------------------------------------------------------------
 
-        addProperty( properties, "maven.build.finalName", PathUtils.toRelative( project.getBasedir(), project
-            .getBuild().getFinalName() ) );
+        writeAntVersionHeader( writer );
 
-        // target
-        addProperty( properties, "maven.build.dir", PathUtils.toRelative( project.getBasedir(), project.getBuild()
-            .getDirectory() ) );
+        // ----------------------------------------------------------------------
+        // <project/>
+        // ----------------------------------------------------------------------
 
-        // ${maven.build.dir}/classes
-        addProperty( properties, "maven.build.outputDir", "${maven.build.dir}/"
-            + PathUtils.toRelative( new File( project.getBasedir(), properties.getProperty( "maven.build.dir" ) ),
-                                    project.getBuild().getOutputDirectory() ) );
-        // src/main/java
-        if ( !project.getCompileSourceRoots().isEmpty() )
-        {
-            String[] compileSourceRoots = (String[]) project.getCompileSourceRoots().toArray( new String[0] );
-            for ( int i = 0; i < compileSourceRoots.length; i++ )
-            {
-                addProperty( properties, "maven.build.srcDir." + i, PathUtils.toRelative( project.getBasedir(),
-                                                                                          compileSourceRoots[i] ) );
-            }
-        }
-        // src/main/resources
-        if ( project.getBuild().getResources() != null )
-        {
-            Resource[] array = (Resource[]) project.getBuild().getResources().toArray( new Resource[0] );
-            for ( int i = 0; i < array.length; i++ )
-            {
-                addProperty( properties, "maven.build.resourceDir." + i, PathUtils.toRelative( project.getBasedir(),
-                                                                                               array[i].getDirectory() ) );
-            }
-        }
+        writer.startElement( "project" );
+        writer.addAttribute( "name", project.getArtifactId() );
+        writer.addAttribute( "default", "jar" );
+        writer.addAttribute( "basedir", "." );
 
-        // ${maven.build.dir}/test-classes
-        addProperty( properties, "maven.build.testOutputDir", "${maven.build.dir}/"
-            + PathUtils.toRelative( new File( project.getBasedir(), properties.getProperty( "maven.build.dir" ) ),
-                                    project.getBuild().getTestOutputDirectory() ) );
-        // src/test/java
-        if ( !project.getTestCompileSourceRoots().isEmpty() )
-        {
-            String[] compileSourceRoots = (String[]) project.getTestCompileSourceRoots().toArray( new String[0] );
-            for ( int i = 0; i < compileSourceRoots.length; i++ )
-            {
-                addProperty( properties, "maven.build.testDir." + i, PathUtils.toRelative( project.getBasedir(),
-                                                                                           compileSourceRoots[i] ) );
-            }
-        }
-        // src/test/resources
-        if ( project.getBuild().getTestResources() != null )
-        {
-            Resource[] array = (Resource[]) project.getBuild().getTestResources().toArray( new Resource[0] );
-            for ( int i = 0; i < array.length; i++ )
-            {
-                addProperty( properties, "maven.build.testResourceDir." + i, PathUtils
-                    .toRelative( project.getBasedir(), array[i].getDirectory() ) );
-            }
-        }
+        AntBuildWriterUtil.writeLineBreak( writer );
 
-        // ----------------------------------------------------------------------
-        // Settings properties
-        // ----------------------------------------------------------------------
+        AntBuildWriterUtil.writeCommentText( writer, "Import " + DEFAULT_MAVEN_BUILD_FILENAME
+            + " into the current project", 1 );
 
-        addProperty( properties, "maven.settings.offline", String.valueOf( settings.isOffline() ) );
-        addProperty( properties, "maven.settings.interactiveMode", String.valueOf( settings.isInteractiveMode() ) );
-        addProperty( properties, "maven.repo.local", localRepository.getAbsolutePath() );
+        writer.startElement( "import" );
+        writer.addAttribute( "file", DEFAULT_MAVEN_BUILD_FILENAME );
+        writer.endElement(); // echo
+
+        AntBuildWriterUtil.writeLineBreak( writer, 1, 1 );
+
+        AntBuildWriterUtil.writeCommentText( writer, "Help target", 1 );
 
-        properties.store( os, "Generated by Maven Ant Plugin" );
+        writer.startElement( "target" );
+        writer.addAttribute( "name", "help" );
+
+        writer.startElement( "echo" );
+        writer.addAttribute( "message", "Please run: $ant -projecthelp" );
+        writer.endElement(); // echo
+
+        writer.endElement(); // target
+
+        AntBuildWriterUtil.writeLineBreak( writer );
+
+        writer.endElement(); // project
+
+        IOUtil.close( w );
     }
 
     private void writeProperties( XMLWriter writer )
@@ -261,7 +357,7 @@ public class AntBuildWriter
         writer.endElement(); // property
 
         writer.startElement( "property" );
-        writer.addAttribute( "file", "build.properties" );
+        writer.addAttribute( "file", DEFAULT_MAVEN_PROPERTIES_FILENAME );
         writer.endElement(); // property
 
         // ----------------------------------------------------------------------
@@ -828,19 +924,13 @@ public class AntBuildWriter
     }
 
     /**
-     * Write comment in the file header
+     * Write comments in the Ant build file header
      *
      * @param writer
      */
     private static void writeHeader( XMLWriter writer )
     {
-        AntBuildWriterUtil.writeLineBreak( writer );
-
-        AntBuildWriterUtil.writeCommentLineBreak( writer );
-        AntBuildWriterUtil.writeComment( writer, "Ant build file (http://ant.apache.org/) for Ant 1.6.2 or above." );
-        AntBuildWriterUtil.writeCommentLineBreak( writer );
-
-        AntBuildWriterUtil.writeLineBreak( writer );
+        writeAntVersionHeader( writer );
 
         AntBuildWriterUtil.writeCommentLineBreak( writer );
         AntBuildWriterUtil.writeComment( writer, StringUtils.repeat( "=", 21 ) + " - DO NOT EDIT THIS FILE! - "
@@ -860,6 +950,17 @@ public class AntBuildWriter
     }
 
     /**
+     * Write comment for the Ant supported version
+     *
+     * @param writer
+     */
+    private static void writeAntVersionHeader( XMLWriter writer )
+    {
+        AntBuildWriterUtil.writeCommentText( writer, "Ant build file (http://ant.apache.org/) for Ant 1.6.2 or above.",
+                                             0 );
+    }
+
+    /**
      * Put a property in properties defined by a name and a value
      *
      * @param properties
diff --git a/src/main/java/org/apache/maven/plugin/ant/AntMojo.java b/src/main/java/org/apache/maven/plugin/ant/AntMojo.java
index f8cb032..f948027 100644
--- a/src/main/java/org/apache/maven/plugin/ant/AntMojo.java
+++ b/src/main/java/org/apache/maven/plugin/ant/AntMojo.java
@@ -26,7 +26,7 @@ import java.io.File;
 import java.io.IOException;
 
 /**
- * Generate an Ant build file.
+ * Generate Ant build files.
  *
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
  * @version $Id$
@@ -63,26 +63,33 @@ public class AntMojo
     private Settings settings;
 
     /**
+     * Overwrite or not the <code>build.xml</code>
+     *
+     * @parameter expression="${overwrite}" default-value="false"
+     */
+    private boolean overwrite;
+
+    /**
      * @see org.apache.maven.plugin.Mojo#execute()
      */
     public void execute()
         throws MojoExecutionException
     {
-        // TODO: read back previous
-
-        AntBuildWriter antBuildWriter = new AntBuildWriter( project, new File( localRepository.getBasedir() ), settings );
+        AntBuildWriter antBuildWriter = new AntBuildWriter( project, new File( localRepository.getBasedir() ),
+                                                            settings, overwrite );
 
         try
         {
-            antBuildWriter.writeBuildXml();
+            antBuildWriter.writeBuildXmls();
             antBuildWriter.writeBuildProperties();
         }
         catch ( IOException e )
         {
-            throw new MojoExecutionException( "Error building Ant script", e );
+            throw new MojoExecutionException( "Error building Ant script:" + e.getMessage(), e );
         }
 
         getLog().info(
-            "Wrote Ant project for " + project.getArtifactId() + " to " + project.getBasedir().getAbsolutePath() );
+                       "Wrote Ant project for " + project.getArtifactId() + " to "
+                           + project.getBasedir().getAbsolutePath() );
     }
 }
diff --git a/src/test/java/org/apache/maven/plugin/ant/AntMojoTest.java b/src/test/java/org/apache/maven/plugin/ant/AntMojoTest.java
index b4f0a1b..dea4437 100644
--- a/src/test/java/org/apache/maven/plugin/ant/AntMojoTest.java
+++ b/src/test/java/org/apache/maven/plugin/ant/AntMojoTest.java
@@ -73,21 +73,27 @@ public class AntMojoTest
     }
 
     /**
-     * @param testPom
+     * Invoke Ant mojo.
+     * <br/>
+     * The Maven test project should be in a directory called <code>testProject</code> in  "src/test/resources/unit/" directory.
+     * The Maven test project should be called <code>"testProject"-plugin-config.xml</code> and should produced
+     * <code>ant-plugin-test.jar</code> as artefact.
+     *
+     * @param testProject
      * @throws Exception
      */
     private void invokeAntMojo( String testProject )
         throws Exception
     {
-        File testPom = new File( getBasedir(),
-                                 "src/test/resources/unit/" + testProject + "/" + testProject + "-plugin-config.xml" );
+        File testPom = new File( getBasedir(), "src/test/resources/unit/" + testProject + "/" + testProject
+            + "-plugin-config.xml" );
         AntMojo mojo = (AntMojo) lookupMojo( "ant", testPom );
         mojo.execute();
 
         File antBasedir = new File( getBasedir(), "target/test/unit/" + testProject + "/" );
         File antBuild = new File( antBasedir, AntBuildWriter.DEFAULT_BUILD_FILENAME );
         assertTrue( antBuild.exists() );
-        File antProperties = new File( antBasedir, AntBuildWriter.DEFAULT_PROPERTIES_FILENAME );
+        File antProperties = new File( antBasedir, AntBuildWriter.DEFAULT_MAVEN_PROPERTIES_FILENAME );
         assertTrue( antProperties.exists() );
 
         AntWrapper.invoke( antBuild );
@@ -97,7 +103,7 @@ public class AntMojoTest
         assertTrue( new File( antBasedir, "target/ant-plugin-test.jar" ).exists() );
 
         Properties properties = new Properties();
-        properties.load( new FileInputStream( new File( getBasedir(), "target/test/unit/" + testProject + "/build.properties" ) ) );
+        properties.load( new FileInputStream( new File( antBasedir, AntBuildWriter.DEFAULT_MAVEN_PROPERTIES_FILENAME ) ) );
         String repo = properties.getProperty( "maven.repo.local" );
         assertTrue( repo.equals( new File( getBasedir(), "target/local-repo" ).getAbsolutePath() ) );
     }

-- 
To stop receiving notification emails like this one, please contact
"commits@maven.apache.org" <co...@maven.apache.org>.