private void generateBuildFile()

in tools/maven/maven-ant-generator/src/main/java/org/apache/tuscany/sca/tools/ant/generator/plugin/AntGeneratorMojo.java [191:382]


    private void generateBuildFile() throws MojoExecutionException {
        
        getLog().info("Generating " + buildFile);
        
        // Open the target build.xml file
        File targetFile = new File(buildFile);
        PrintWriter pw;
        try {
            pw = new PrintWriter(new FileOutputStream(targetFile));
        } catch (FileNotFoundException e) {
            throw new MojoExecutionException(e.toString());
        }

        // Determine the project packaging
        String packaging = project.getPackaging().toLowerCase();

        // Generate the Apache license header
        generateLicenseHeader(pw);
        
        pw.println("<project name=\"" + project.getArtifactId() + "\" default=\"compile\">");
        pw.println();
        
        // Generate the compile target
        int base = project.getBasedir().toString().length() + 1;
        pw.println("    <target name=\"compile\">");
        pw.println("        <mkdir dir=\"target/classes\"/>");

        // Generate any pre-compilation tasks
        generatePreCompileTasks(pw);
        
        // Generate the compile task
        pw.println("        <javac destdir=\"target/classes\" debug=\"on\" source=\"1.5\" target=\"1.5\">");
        for (String source: (List<String>)project.getCompileSourceRoots()) {
            if (source.length() > base) {
                source = source.substring(base);
            } else {
                source = ".";
            }
            pw.println("            <src path=\"" + source + "\"/>");
        }
        pw.println("            <classpath>");
        pw.println("                <fileset refid=\"tuscany.jars\"/>");
        pw.println("                <fileset refid=\"3rdparty.jars\"/>");
        pw.println("            </classpath>");
        pw.println("        </javac>");
        pw.println("        <copy todir=\"target/classes\">");
        for (FileSet resource: (List<FileSet>)project.getResources()) {
            String source = resource.getDirectory();
            if (source.length() > base) {
                source = source.substring(base);
                
                if (source.equals(".")){
                    pw.println("            <fileset dir=\".\" includes=\"*\" excludes=\"src, target, pom.xml, build.xml\"/>");
                } else {
                    pw.println("            <fileset dir=\"" + source + "\"/>");
                }
            } else {
                if (project.getResources().size() > 1) {
                    break;
                }
                pw.println("            <fileset dir=\".\" excludes=\"**/*.java, **/.*/**, pom.xml, build*.xml, target/**\"/>");
                source = ".";
            }
        }
        pw.println("        </copy>");
        
        // Compile test source using code cut and pasted from above
        if (testSource) {
            pw.println("        <mkdir dir=\"target/test-classes\"/>");
            pw.println("        <javac destdir=\"target/test-classes\" debug=\"on\" source=\"1.5\" target=\"1.5\">");
            for (String source: (List<String>)project.getTestCompileSourceRoots()) {
                if (source.length() > base) {
                    source = source.substring(base);
                } else {
                    source = ".";
                }
                pw.println("            <src path=\"" + source + "\"/>");
            }
            pw.println("            <classpath>");
            pw.println("                <fileset dir=\"target/classes\"/>");
            pw.println("                <fileset refid=\"tuscany.jars\"/>");
            pw.println("                <fileset refid=\"3rdparty.jars\"/>");
            pw.println("            </classpath>");
            pw.println("        </javac>");
            pw.println("        <copy todir=\"target/test-classes\">");
            for (FileSet resource: (List<FileSet>)project.getTestResources()) {
                String source = resource.getDirectory();
                if (source.length() > base) {
                    source = source.substring(base);
                    
                    if (source.equals(".")){
                        pw.println("            <fileset dir=\".\" includes=\"*\" excludes=\"src, target, pom.xml, build.xml\"/>");
                    } else {
                        pw.println("            <fileset dir=\"" + source + "\"/>");
                    }
                } else {
                    if (project.getTestResources().size() > 1) {
                        break;
                    }
                    pw.println("            <fileset dir=\".\" excludes=\"**/*.java, **/.*/**, pom.xml, build*.xml, target/**\"/>");
                    source = ".";
                }
            }
            pw.println("        </copy>");
        }
        
        // Build a JAR
        if (packaging.equals("jar")) {
            pw.println("        <jar destfile=\"target/" + project.getArtifactId() + ".jar\" basedir=\"target/classes\">");
            pw.println("            <manifest>");
            if (mainClass != null) {
                pw.println("                <attribute name=\"Main-Class\" value=\"" + mainClass + "\"/>");
            }
            pw.println("            </manifest>");
            pw.println("        </jar>");
            
        } else if (packaging.equals("war")) {
            
            // Build a WAR
            pw.println("        <war destfile=\"target/" + project.getArtifactId() + ".war\" webxml=\"src/main/webapp/WEB-INF/web.xml\">");
            pw.println("            <fileset dir=\"src/main/webapp\"/>");
            pw.println("            <lib refid=\"tuscany.jars\"/>");
            pw.println("            <lib refid=\"3rdparty.jars\"/>");
            pw.println("            <classes dir=\"target/classes\"/>");
            pw.println("        </war>");
        }

        // Build additional JARs
        if (jarFiles != null) {
            for (JarFile jarFile: jarFiles) {
                pw.println("        <jar destfile=\"" + jarFile.getDestfile() + "\">");
                if (jarFile.getFilesets() != null) {
                    for (String fileset: jarFile.getFilesets()) {
                        pw.println("            <fileset " + fileset + "/>");
                    }
                }
                pw.println("        </jar>");
            }
        }

        // Finish the compile target
        pw.println("    </target>");
        pw.println();
        
        
        // Generate a package target alongside the compile target 
        // Tuscany SCA samples use "package" as the target for webapps
        pw.println("    <target name=\"package\" depends=\"compile\"/>");
        pw.println();
    
        // Generate the run target
        if (mainClass != null) {
            pw.println("    <target name=\"run\">");
            pw.println("        <java classname=\"" + mainClass + "\" fork=\"true\">");
            pw.println("            <classpath>");
            pw.println("                <pathelement location=\"target/" + project.getArtifactId() + ".jar\"/>");
            pw.println("                <fileset refid=\"tuscany.jars\"/>");
            pw.println("                <fileset refid=\"3rdparty.jars\"/>");
            pw.println("            </classpath>");
            pw.println("        </java>");
            pw.println("    </target>");
            pw.println();
        }

        // Generate other run targets
        if (runTargets != null) {
            for (Map.Entry<String, String> element: runTargets.entrySet()) {
                pw.println("    <target name=\"" + element.getKey() + "\">");
                pw.println("        <java classname=\"" + element.getValue() + "\" fork=\"true\">");
                pw.println("            <classpath>");
                pw.println("                <pathelement location=\"target/" + project.getArtifactId() + ".jar\"/>");
                pw.println("                <fileset refid=\"tuscany.jars\"/>");
                pw.println("                <fileset refid=\"3rdparty.jars\"/>");
                pw.println("            </classpath>");
                pw.println("        </java>");
                pw.println("    </target>");
                pw.println();
            }
        }
        
        // Generate the clean target
        pw.println("    <target name=\"clean\">");
        pw.println("        <delete dir=\"target\" includeemptydirs=\"true\"/>");
        pw.println("    </target>");
        pw.println();

        // Generate Ant filesets representing the build dependencies
        generateBuildDependencies(pw);
        
        pw.println("</project>");
        pw.close();
    }