You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by vs...@apache.org on 2006/09/24 15:47:53 UTC

svn commit: r449404 - in /maven/plugins/trunk/maven-ant-plugin/src: main/java/org/apache/maven/plugin/ant/AntBuildWriter.java main/java/org/apache/maven/plugin/ant/AntMojo.java test/java/org/apache/maven/plugin/ant/AntMojoTest.java

Author: vsiveton
Date: Sun Sep 24 06:47:52 2006
New Revision: 449404

URL: http://svn.apache.org/viewvc?view=rev&rev=449404
Log:
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

Modified:
    maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java
    maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntMojo.java
    maven/plugins/trunk/maven-ant-plugin/src/test/java/org/apache/maven/plugin/ant/AntMojoTest.java

Modified: maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java?view=diff&rev=449404&r1=449403&r2=449404
==============================================================================
--- maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java (original)
+++ maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntBuildWriter.java Sun Sep 24 06:47:52 2006
@@ -42,7 +42,12 @@
 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 @@
 
     /**
      * 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 @@
 
     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 @@
         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 @@
         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 @@
     }
 
     /**
-     * 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! - "
@@ -857,6 +947,17 @@
         AntBuildWriterUtil.writeCommentLineBreak( writer );
 
         AntBuildWriterUtil.writeLineBreak( writer );
+    }
+
+    /**
+     * 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 );
     }
 
     /**

Modified: maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntMojo.java?view=diff&rev=449404&r1=449403&r2=449404
==============================================================================
--- maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntMojo.java (original)
+++ maven/plugins/trunk/maven-ant-plugin/src/main/java/org/apache/maven/plugin/ant/AntMojo.java Sun Sep 24 06:47:52 2006
@@ -26,7 +26,7 @@
 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 @@
     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() );
     }
 }

Modified: maven/plugins/trunk/maven-ant-plugin/src/test/java/org/apache/maven/plugin/ant/AntMojoTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-ant-plugin/src/test/java/org/apache/maven/plugin/ant/AntMojoTest.java?view=diff&rev=449404&r1=449403&r2=449404
==============================================================================
--- maven/plugins/trunk/maven-ant-plugin/src/test/java/org/apache/maven/plugin/ant/AntMojoTest.java (original)
+++ maven/plugins/trunk/maven-ant-plugin/src/test/java/org/apache/maven/plugin/ant/AntMojoTest.java Sun Sep 24 06:47:52 2006
@@ -73,21 +73,27 @@
     }
 
     /**
-     * @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 @@
         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() ) );
     }