Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.apache.maven.plugin.ant;
  
  /*
   * Copyright 2001-2005 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
  *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 import  org.apache.maven.project.MavenProject;
 import  org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 import  org.codehaus.plexus.util.xml.XMLWriter;
 
 import java.io.File;
 import java.io.Writer;
 import java.util.List;

Author(s):
Brett Porter
Version:
$Id: AntBuildWriter.java 233431 2005-08-19 04:27:34Z brett $
 
 public class AntBuildWriter
 {
     private MavenProject project;
 
     private File localRepository;
 
     public AntBuildWriter( MavenProject projectFile localRepository )
     {
         this. = project;
         this. = localRepository;
     }
 
     public void write()
         throws AntPluginException
     {
         writeBuildXml();
 
         ..println(
             "Wrote Ant project for " + .getArtifactId() + " to " + .getBasedir().getAbsolutePath() );
     }
 
     // ----------------------------------------------------------------------
     // build.xml
     // ----------------------------------------------------------------------
 
     protected void writeBuildXml()
         throws AntPluginException
     {
         FileWriter w;
 
         try
         {
             // TODO: parameter
             w = new FileWriternew File.getBasedir(), "build.xml" ) );
         }
         catch ( IOException ex )
         {
             throw new AntPluginException"Exception while opening file."ex );
         }
 
         XMLWriter writer = new PrettyPrintXMLWriter( w );
 
         writer.startElement( "project" );
         writer.addAttribute( "name".getArtifactId() );
         writer.addAttribute( "default""jar" );
         writer.addAttribute( "basedir""." );
 
         writePropertieswriter );
 
         writeBuildPathDefinitionwriter );
 
         writeCleanTargetwriter );
         List compileSourceRoots = removeEmptyCompileSourceRoots.getCompileSourceRoots() );
         writeCompileTargetwritercompileSourceRoots );
 
         // TODO: what if type is not JAR?
         writeJarTargetwriter );
 
         List testCompileSourceRoots = removeEmptyCompileSourceRoots.getTestCompileSourceRoots() );
         writeCompileTestsTargetwritertestCompileSourceRoots );
         writeTestTargetswritertestCompileSourceRoots );
        writeGetDepsTargetwriter );
        writer.endElement(); // project
        closew );
    }
    private void writeCompileTestsTarget( XMLWriter writerList testCompileSourceRoots )
    {
        writer.startElement( "target" );
        writer.addAttribute( "name""compile-tests" );
        writer.addAttribute( "depends""junit-present, compile" );
        writer.addAttribute( "description""Compile the test code" );
        writer.addAttribute( "if""junit.present" );
        writeCompileTaskswriter.getBasedir(), "${maven.test.output}"testCompileSourceRoots,
                           .getBuild().getTestResources(), "${maven.build.output}" );
        writer.endElement(); // target
    }
    private void writeTestTargets( XMLWriter writerList testCompileSourceRoots )
    {
        writer.startElement( "target" );
        writer.addAttribute( "name""test" );
        writer.addAttribute( "depends""junit-present, compile-tests" );
        writer.addAttribute( "if""junit.present" );
        writer.addAttribute( "description""Run the test cases" );
        if ( !testCompileSourceRoots.isEmpty() )
        {
            writer.startElement( "mkdir" );
            writer.addAttribute( "dir""${maven.test.reports}" );
            writer.endElement(); //mkdir
            writer.startElement( "junit" );
            writer.addAttribute( "printSummary""yes" );
            writer.addAttribute( "haltonerror""true" );
            writer.addAttribute( "haltonfailure""true" );
            writer.addAttribute( "fork""true" );
            writer.addAttribute( "dir""." );
            writer.startElement( "sysproperty" );
            writer.addAttribute( "key""basedir" );
            writer.addAttribute( "value""." );
            writer.endElement(); // sysproperty
            writer.startElement( "formatter" );
            writer.addAttribute( "type""xml" );
            writer.endElement(); // formatter
            writer.startElement( "formatter" );
            writer.addAttribute( "type""plain" );
            writer.addAttribute( "usefile""false" );
            writer.endElement(); // formatter
            writer.startElement( "classpath" );
            writer.startElement( "path" );
            writer.addAttribute( "refid""build.classpath" );
            writer.endElement(); // path
            writer.startElement( "pathelement" );
            writer.addAttribute( "location""${maven.build.output}" );
            writer.endElement(); // pathelement
            writer.startElement( "pathelement" );
            writer.addAttribute( "location""${maven.test.output}" );
            writer.endElement(); // pathelement
            writer.endElement(); // classpath
            writer.startElement( "batchtest" );
            writer.addAttribute( "todir""${maven.test.reports}" );
            for ( Iterator i = testCompileSourceRoots.iterator(); i.hasNext(); )
            {
                writer.startElement( "fileset" );
                String testSrcDir = (Stringi.next();
                writer.addAttribute( "dir"toRelative.getBasedir(), testSrcDir ) );
/* TODO: need to get these from the test plugin somehow?
                UnitTest unitTest = project.getBuild().getUnitTest();
                writeIncludesExcludes( writer, unitTest.getIncludes(), unitTest.getExcludes() );
                // TODO: m1 allows additional test exclusions via maven.ant.excludeTests
*/
                writeIncludesExcludeswriter, Collections.singletonList"**/*Test.java" ),
                                       Collections.singletonList"**/*Abstract*Test.java" ) );
                writer.endElement(); // fileset
            }
            writer.endElement(); // batchtest
            writer.endElement(); // junit
        }
        writer.endElement(); // target
        writer.startElement( "target" );
        writer.addAttribute( "name""test-junit-present" );
        writer.startElement( "available" );
        writer.addAttribute( "classname""junit.framework.Test" );
        writer.addAttribute( "property""junit.present" );
        writer.endElement(); // available
        writer.endElement(); // target
        writer.startElement( "target" );
        writer.addAttribute( "name""junit-present" );
        writer.addAttribute( "depends""test-junit-present" );
        writer.addAttribute( "unless""junit.present" );
        writer.startElement( "echo" );
        writer.writeText( "================================= WARNING ================================" );
        writer.endElement(); // echo
        writer.startElement( "echo" );
        writer.writeText( " Junit isn't present in your $ANT_HOME/lib directory. Tests not executed. " );
        writer.endElement(); // echo
        writer.startElement( "echo" );
        writer.writeText( "==========================================================================" );
        writer.endElement(); // echo
        writer.endElement(); // target
    }
    private void writeJarTarget( XMLWriter writer )
    {
        writer.startElement( "target" );
        writer.addAttribute( "name""jar" );
        writer.addAttribute( "depends""compile,test" );
        writer.addAttribute( "description""Clean the JAR" );
        writer.startElement( "jar" );
        writer.addAttribute( "jarfile""${maven.build.directory}/${maven.build.final.name}.jar" );
        writer.addAttribute( "basedir""${maven.build.output}" );
        writer.addAttribute( "excludes""**/package.html" );
        writer.endElement(); // jar
        writer.endElement(); // target
    }
    private void writeCleanTarget( XMLWriter writer )
    {
        writer.startElement( "target" );
        writer.addAttribute( "name""clean" );
        writer.addAttribute( "description""Clean the output directory" );
        writer.startElement( "delete" );
        writer.addAttribute( "dir""${maven.build.directory}" );
        writer.endElement(); // delete
        writer.endElement(); // target
    }
    private void writeCompileTarget( XMLWriter writerList compileSourceRoots )
    {
        writer.startElement( "target" );
        writer.addAttribute( "name""compile" );
        writer.addAttribute( "depends""get-deps" );
        writer.addAttribute( "description""Compile the code" );
        writeCompileTaskswriter.getBasedir(), "${maven.build.output}"compileSourceRoots,
                           .getBuild().getResources(), null );
        writer.endElement(); // target
    }
    private static void writeCompileTasks( XMLWriter writerFile basedirString outputDirectory,
                                           List compileSourceRootsList resourcesString additionalClassesDirectory )
    {
        writer.startElement( "mkdir" );
        writer.addAttribute( "dir"outputDirectory );
        writer.endElement(); // mkdir
        if ( !compileSourceRoots.isEmpty() )
        {
            writer.startElement( "javac" );
            writer.addAttribute( "destdir"outputDirectory );
            writer.addAttribute( "excludes""**/package.html" );
            writer.addAttribute( "debug""true" ); // TODO: use compiler setting
            writer.addAttribute( "deprecation""true" ); // TODO: use compiler setting
            writer.addAttribute( "optimize""false" ); // TODO: use compiler setting
            for ( Iterator i = compileSourceRoots.iterator(); i.hasNext(); )
            {
                String srcDir = (Stringi.next();
                writer.startElement( "src" );
                writer.startElement( "pathelement" );
                writer.addAttribute( "location"toRelativebasedirsrcDir ) );
                writer.endElement(); // pathelement
                writer.endElement(); // src
            }
            if ( additionalClassesDirectory == null )
            {
                writer.startElement( "classpath" );
                writer.addAttribute( "refid""build.classpath" );
                writer.endElement(); // classpath
            }
            else
            {
                writer.startElement( "classpath" );
                writer.startElement( "path" );
                writer.addAttribute( "refid""build.classpath" );
                writer.endElement(); // path
                writer.startElement( "pathelement" );
                writer.addAttribute( "location"additionalClassesDirectory );
                writer.endElement(); // pathelement
                writer.endElement(); // classpath
            }
            writer.endElement(); // javac
        }
        for ( Iterator i = resources.iterator(); i.hasNext(); )
        {
            Resource resource = (Resourcei.next();
            if ( new Fileresource.getDirectory() ).exists() )
            {
                String outputDir = outputDirectory;
                if ( resource.getTargetPath() != null && resource.getTargetPath().length() > 0 )
                {
                    outputDir = outputDir + "/" + resource.getTargetPath();
                    writer.startElement( "mkdir" );
                    writer.addAttribute( "dir"outputDir );
                    writer.endElement(); // mkdir
                }
                writer.startElement( "copy" );
                writer.addAttribute( "todir"outputDir );
                writer.startElement( "fileset" );
                writer.addAttribute( "dir"toRelativebasedirresource.getDirectory() ) );
                writeIncludesExcludeswriterresource.getIncludes(), resource.getExcludes() );
                writer.endElement(); // fileset
                writer.endElement(); // copy
            }
        }
    }
    private static List removeEmptyCompileSourceRootsList compileSourceRoots )
    {
        List newCompileSourceRootsList = new ArrayList();
        if ( compileSourceRoots != null )
        {
            // copy as I may be modifying it
            for ( Iterator i = compileSourceRoots.iterator(); i.hasNext(); )
            {
                String srcDir = (Stringi.next();
                if ( new FilesrcDir ).exists() )
                {
                    newCompileSourceRootsList.addsrcDir );
                }
            }
        }
        return newCompileSourceRootsList;
    }
    private static void writeIncludesExcludes( XMLWriter writerList includesList excludes )
    {
        for ( Iterator i = includes.iterator(); i.hasNext(); )
        {
            String include = (Stringi.next();
            writer.startElement( "include" );
            writer.addAttribute( "name"include );
            writer.endElement(); // include
        }
        for ( Iterator i = excludes.iterator(); i.hasNext(); )
        {
            String exclude = (Stringi.next();
            writer.startElement( "exclude" );
            writer.addAttribute( "name"exclude );
            writer.endElement(); // exclude
        }
    }
    private void writeGetDepsTarget( XMLWriter writer )
    {
        writer.startElement( "target" );
        writer.addAttribute( "name""test-offline" );
        writer.startElement( "condition" );
        writer.addAttribute( "property""maven.mode.offline" );
        writer.startElement( "equals" );
        writer.addAttribute( "arg1""${build.sysclasspath}" );
        writer.addAttribute( "arg2""only" );
        writer.endElement(); // equals
        writer.endElement(); // condition
        writer.endElement(); // target
        writer.startElement( "target" );
        writer.addAttribute( "name""get-deps" );
        writer.addAttribute( "depends""test-offline" );
        writer.addAttribute( "description""Download all dependencies" );
        writer.addAttribute( "unless""maven.mode.offline" ); // TODO: check, and differs from m1
        writer.startElement( "mkdir" );
        writer.addAttribute( "dir""${maven.repo.local}" );
        writer.endElement(); // mkdir
        // TODO: proxy - probably better to use wagon!
        for ( Iterator i = .getArtifacts().iterator(); i.hasNext(); )
        {
            Artifact artifact = (Artifacti.next();
            // TODO: should the artifacthandler be used instead?
            String path = toRelativeartifact.getFile().getPath() );
            for ( Iterator j = .getRepositories().iterator(); j.hasNext(); )
            {
                Repository repository = (Repositoryj.next();
                writer.startElement( "get" );
                writer.addAttribute( "src"repository.getUrl() + "/" + path );
                writer.addAttribute( "dest""${maven.repo.local}/" + path );
                writer.addAttribute( "usetimestamp""true" );
                writer.addAttribute( "ignoreerrors""true" );
                writer.endElement(); // get
            }
        }
        writer.endElement(); // target
    }
    private void writeBuildPathDefinition( XMLWriter writer )
    {
        writer.startElement( "path" );
        writer.addAttribute( "id""build.classpath" );
        writer.startElement( "fileset" );
        writer.addAttribute( "dir""${maven.repo.local}" );
        for ( Iterator i = .getArtifacts().iterator(); i.hasNext(); )
        {
            Artifact artifact = (Artifacti.next();
            writer.startElement( "include" );
            writer.addAttribute( "name"toRelativeartifact.getFile().getPath() ) );
            writer.endElement(); // include
        }
        writer.endElement(); // fileset
        writer.endElement(); // path
    }
    private void writeProperties( XMLWriter writer )
    {
        // TODO: optional in m1
        // TODO: USD properties
        writer.startElement( "property" );
        writer.addAttribute( "file""${user.home}/.m2/maven.properties" );
        writer.endElement(); // property
        writer.startElement( "property" );
        writer.addAttribute( "name""maven.build.output" );
        writer.addAttribute( "value"toRelative.getBasedir(), .getBuild().getOutputDirectory() ) );
        writer.endElement(); // property
        writer.startElement( "property" );
        writer.addAttribute( "name""maven.build.directory" );
        writer.addAttribute( "value"toRelative.getBasedir(), .getBuild().getDirectory() ) );
        writer.endElement(); // property
        writer.startElement( "property" );
        writer.addAttribute( "name""maven.build.final.name" );
        writer.addAttribute( "value".getBuild().getFinalName() );
        writer.endElement(); // property
        // TODO: property?
        writer.startElement( "property" );
        writer.addAttribute( "name""maven.test.reports" );
        writer.addAttribute( "value""${maven.build.directory}/test-reports" );
        writer.endElement(); // property
        writer.startElement( "property" );
        writer.addAttribute( "name""maven.test.output" );
        writer.addAttribute( "value"toRelative.getBasedir(), .getBuild().getTestOutputDirectory() ) );
        writer.endElement(); // property
        writer.startElement( "property" );
        writer.addAttribute( "name""maven.repo.local" );
        writer.addAttribute( "value""${user.home}/.m2/repository" );
        writer.endElement(); // property
/* TODO: offline setting
        writer.startElement( "property" );
        writer.addAttribute( "name", "maven.mode.offline" );
        writer.addAttribute( "value", project.getBuild().getOutput() );
        writer.endElement(); // property
*/
    }
    private void closeWriter closeable )
    {
        if ( closeable == null )
        {
            return;
        }
        try
        {
            closeable.close();
        }
        catch ( Exception e )
        {
            // ignore
            // TODO: warn
        }
    }
    // TODO: move to plexus-utils or use something appropriate from there (eclipse plugin too)
    private static String toRelativeFile basedirString absolutePath )
    {
        String relative;
        absolutePath = absolutePath.replace'\\''/' );
        String basedirPath = basedir.getAbsolutePath().replace'\\''/' );
        if ( absolutePath.startsWithbasedirPath ) )
        {
            relative = absolutePath.substringbasedirPath.length() + 1 );
        }
        else
        {
            relative = absolutePath;
        }
        return relative;
    }
New to GrepCode? Check out our FAQ X