summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/tags/1.0.1/modules/maven-ant-generator/src/main/java/org/apache/tuscany/tools/ant/generator/plugin/AntGeneratorMojo.java
blob: 32838a6c5cb33c2d5102493172cd340f82bdec3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.    
 */
package org.apache.tuscany.tools.ant.generator.plugin;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Settings;

/**
 * @version $Rev$ $Date$
 * @goal generate
 * @phase generate-sources
 * @requiresDependencyResolution test
 * @description Generate Ant build script for an SCA project
 */
public class AntGeneratorMojo extends AbstractMojo {
    /**
     * The project to create a build for.
     *
     * @parameter expression="${project}"
     * @required
     */
    private MavenProject project;

    /**
     * Used for resolving artifacts
     *
     * @component
     */
    private ArtifactResolver resolver;

    /**
     * Factory for creating artifact objects
     *
     * @component
     */
    private ArtifactFactory factory;

    /**
     * The local repository where the artifacts are located
     *
     * @parameter expression="${localRepository}"
     * @required
     */
    private ArtifactRepository localRepository;

    /**
     * The remote repositories where artifacts are located
     *
     * @parameter expression="${project.remoteArtifactRepositories}"
     */
    private List remoteRepositories;

    /**
     * The current user system settings for use in Maven.
     *
     * @parameter expression="${settings}"
     * @required
     * @readonly
     */
    private Settings settings;

    /**
     * The main class name.
     * @parameter
     */
    private String mainClass;
    
    /**
     * The build.xml file to generate.
     * @parameter expression="${basedir}/build.xml"
     */
    private String buildFile;
    
    public void execute() throws MojoExecutionException {
        
        System.out.println("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();
        
        // Determine the module dependencies
        List<Artifact> tuscanyModules = new ArrayList<Artifact>();
        List<Artifact> otherModules = new ArrayList<Artifact>();
        for (Artifact artifact: (List<Artifact>)project.getRuntimeArtifacts()) {
            if (artifact.getGroupId().startsWith("org.apache.tuscany.sca")) {
                tuscanyModules.add(artifact);
            } else {
                otherModules.add(artifact);
            }
        }
        
        pw.println("<project name=\"" + project.getArtifactId() + "\" default=\"compile\">");
        pw.println();
        
        // Generate the compile target
        pw.println("    <target name=\"compile\">");
        pw.println("        <javac srcdir=\"src/main/java\" destdir=\"target/classes\" debug=\"on\" source=\"1.5\" target=\"1.5\">");
        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\">");
        pw.println("            <fileset dir=\"src/main/resources\"/>");
        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>");
        }
        pw.println("    </target>");
        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 the clean target
        pw.println("    <target name=\"clean\">");
        pw.println("        <delete quiet=\"true\" includeemptydirs=\"true\">");
        pw.println("            <fileset dir=\"target\"/>");
        pw.println("        </delete>");
        pw.println("    </target>");
        pw.println();
    
        // Generate the classpath
        pw.println("    <fileset id=\"tuscany.jars\" dir=\"../../modules\">");
        for (Artifact artifact: tuscanyModules) {
            pw.println("        <include name=\"" + artifact.getFile().getName() +"\"/>");
        }
        pw.println("    </fileset>");
        pw.println("    <fileset id=\"3rdparty.jars\" dir=\"../../lib\">");
        for (Artifact artifact: otherModules) {
            pw.println("        <include name=\"" + artifact.getFile().getName() +"\"/>");
        }
        pw.println("    </fileset>");
        pw.println();
        
        pw.println("</project>");
        pw.close();
    }

}