summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/branches/sca-java-1.6.2/tools/maven/maven-incremental-build/src/main/java/org/apache/tuscany/sca/tools/incremental/build/plugin/IncrementalBuildMojo.java
blob: 3a6870732b8116ec12e9c65171f0eba58e121352 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
 * 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.sca.tools.incremental.build.plugin;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Settings;
import org.apache.maven.shared.invoker.DefaultInvocationRequest;
import org.apache.maven.shared.invoker.InvocationResult;
import org.apache.maven.shared.invoker.Invoker;
import org.apache.maven.shared.invoker.MavenInvocationException;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.cli.CommandLineException;

/**
 * @version $Rev$ $Date$
 * @goal build
 * @phase validate
 * @requiresDependencyResolution test
 * @description Incrementally build project modules that depend on modified modules.
 */
public class IncrementalBuildMojo extends AbstractMojo {

    /**
     * Keep track of modified projects.
     */
    private static Set<String> modifiedProjectIDs = new HashSet<String>();

    /**
     * The current user system settings for use in Maven.
     *
     * @parameter expression="${settings}"
     * @required
     * @readonly
     */
    private Settings settings;
    
    /**
     * Used to invoke Maven builds.
     *
     * @component
     */
    private Invoker invoker;

    /**
     * The target directory of the compiler if fork is true.
     *
     * @parameter expression="${project.build.directory}"
     * @required
     * @readonly
     */
    private File buildDirectory;

    /**
     * The project to create a build for.
     *
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;

    private File outputFile;

    public void execute() throws MojoExecutionException {
        String projectID = id(project);
        outputFile = getOutputFile();

        File testMarkerFile = new File(project.getBasedir().getPath() + "/.test");
        
        List<String> goals = new ArrayList<String>();
        String type = project.getArtifact().getType();
        if ("pom".equals(type)) {
            
            // Always install pom modules
            goals.add("install");

        } else {

            // Check if anything has changed in the project
            boolean changed = false;
            boolean testChanged = false;
            boolean testFailed = false;
            if (new File(project.getBasedir().getPath() + "/.modified").exists()) {
                getLog().info("Found .modified marker file.");
                changed = true;
            } else {
                changed = areSourcesStale() || areResourcesStale() || isPOMStale();
            }
            if (changed) {
                modifiedProjectIDs.add(projectID);
            } else {
                testChanged = areTestSourcesStale() || areTestResourcesStale();
            }
            
            // Check if a project has compile dependencies on the modified projects
            // and will need to be recompiled, or has runtime or test dependencies
            // on the modified projects and needs to be retested
            if (changed) {
                goals.add("clean");
                goals.add("install");
                getLog().info("Project " + projectID + " has changed and will be recompiled.");
                
            } else {
                for (Artifact artifact : (List<Artifact>)project.getCompileArtifacts()) {
                    String artifactID = id(artifact);
                    if (modifiedProjectIDs.contains(artifactID)) {
                        getLog().info("Project " + projectID + " depends on modified project " + artifactID + " and will be recompiled.");
                        goals.add("clean");
                        goals.add("install");
                        break;
                    }
                }

                if (goals.isEmpty()) {
                    List<Artifact> artifacts = new ArrayList<Artifact>();
                    artifacts.addAll(project.getRuntimeArtifacts());
                    artifacts.addAll(project.getTestArtifacts());
                    for (Artifact artifact : artifacts) {
                        String artifactID = id(artifact);
                        if (modifiedProjectIDs.contains(artifactID)) {
                            getLog().info("Project " + projectID + " depends on modified project " + artifactID + " and will be retested.");
                            goals.add("test");
                            break;
                        }
                    }
                }
            }

            if (testChanged && goals.isEmpty()) {
                getLog().info("Project " + projectID + " has changed and will be retested.");
                goals.add("test");
            }

            if (goals.isEmpty()) {
                if (testMarkerFile.exists()) {
                    testFailed = true;
                    getLog().info("Project " + projectID + " contains failed tests and will be retested.");
                    goals.add("test");
                }
            }
        }

        // Invoke Maven with the necessary goals
        if (!goals.isEmpty()) {
            DefaultInvocationRequest request = new DefaultInvocationRequest();
            request.setGoals(goals);
            // FIXME: The maven invoker doesn't handle the directory names with spaces
            // request.setLocalRepositoryDirectory(new File(localRepository.getBasedir()));
            request.setInteractive(false);
            request.setShowErrors(false);
            request.setRecursive(false);
            // request.setDebug(true);
            request.setOffline(settings.isOffline());
            request.setBaseDirectory(project.getBasedir());
            request.setPomFile(project.getFile());

            boolean success = false;
            try {
                try {
                    InvocationResult result = invoker.execute(request);
    
                    CommandLineException cle = result.getExecutionException();
                    if (cle != null) {
                        throw new MojoExecutionException(cle.getMessage(), cle);
                    }
    
                    int ec = result.getExitCode();
                    if (ec != 0) {
                        throw new MojoExecutionException("Maven invocation exit code: " + ec);
                    }
                    
                    success = true;
                    
                    
                    
                } catch (MavenInvocationException e) {
                    throw new MojoExecutionException(e.getMessage(), e);
                }
            } finally {
                
                // Create or delete a .test marker file to keep track of the latest
                // test result status and trigger testing again next time the build
                // is run
                if (!success) {
                    try {
                        if (!testMarkerFile.exists()) {
                            testMarkerFile.createNewFile();
                        }
                    } catch (IOException e) {
                        throw new MojoExecutionException(e.getMessage(), e);
                    }
                } else {
                    if (testMarkerFile.exists()) {
                        testMarkerFile.delete();
                    }
                }
            }
        } else {
            getLog().info("The project is up-to-date. No build is required.");
        }
    }

    private File getOutputFile() {
        File basedir = buildDirectory;
        String finalName = project.getBuild().getFinalName();
        String classifier = project.getArtifact().getClassifier();
        if (classifier == null) {
            classifier = "";
        } else if (classifier.trim().length() > 0 && !classifier.startsWith("-")) {
            classifier = "-" + classifier;
        }
    
        String pkg = project.getPackaging();
        if ("maven-plugin".equals(pkg)) {
            pkg = "jar";
        }
        return new File(basedir, finalName + classifier + "." + pkg);
    }

    /**
     * Test if any of the resources are stale
     * @param resources
     * @param outputDirectory
     * @return
     * @throws MojoExecutionException
     */
    private boolean areStale(List<Resource> resources, String outputDirectory) throws MojoExecutionException {
        
        for (Resource resource: resources) {
            
            File resourceDirectory = new File(resource.getDirectory());
            if (!resourceDirectory.exists()) {
                continue;
            }
    
            DirectoryScanner scanner = new DirectoryScanner();
    
            scanner.setBasedir(resource.getDirectory());
            if (resource.getIncludes() != null && !resource.getIncludes().isEmpty()) {
                scanner.setIncludes((String[])resource.getIncludes().toArray(new String[]{}));
            } else {
                scanner.setIncludes(new String[]{"**/**"});
            }
            if (resource.getExcludes() != null && !resource.getExcludes().isEmpty()) {
                scanner.setExcludes((String[])resource.getExcludes().toArray(new String[]{}));
            }

            scanner.addDefaultExcludes();
            scanner.scan();
    
            List<String> includedFiles = Arrays.asList(scanner.getIncludedFiles());
            String targetPath = resource.getTargetPath();
            for (String source: includedFiles) {
                String target;
                if (source.endsWith(".java")) {
                    target = source.substring(0, source.length() - 5) + ".class";
                } else {
                    target = source;
                }
                
                String destination;
                if (targetPath != null) {
                    destination = targetPath + "/" + target;
                } else {
                    destination = target;
                }
    
                File sourceFile = new File(resource.getDirectory(), source);
                File destinationFile = new File(outputDirectory, destination);
                
                if (!destinationFile.exists()) {
                    getLog().info("Source file " + sourceFile + ".");
                    getLog().info("Target file " + destinationFile + " could not be found.");
                    return true;
                } else {
                    if (sourceFile.lastModified() > destinationFile.lastModified()) {
                        getLog().info("Source file " + sourceFile + " has changed.");
                        getLog().info("Target file " + destinationFile + " is stale.");
                        return true;
                    } else if (sourceFile.lastModified() > outputFile.lastModified()) {
                        getLog().info("Source file " + sourceFile + " has changed.");
                        getLog().info("Target build output file " + outputFile + " is stale.");
                        return true;
                    } else if (outputFile.lastModified() == 0) {
                        getLog().info("Target build output file " + outputFile + " could not be found.");
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * Test if the POM resource is stale.
     * 
     * @return
     */
    private boolean isPOMStale() {
        File pom = project.getFile();
        if (pom.lastModified() > outputFile.lastModified()) {
            getLog().info("File " + pom + " has changed.");
            getLog().info("Target build output file " + pom + " is stale.");
            return true;
        } else if (outputFile.lastModified() == 0) {
            getLog().info("Target build output file " + outputFile + " could not be found.");
            return true;
        } else {
            return false;
        }
    }

    /**
     * Test if the project resources are stale.
     * 
     * @return
     * @throws MojoExecutionException
     */
    private boolean areResourcesStale() throws MojoExecutionException {
        return areStale(project.getResources(), project.getBuild().getOutputDirectory());
    }

    /**
     * Test if the project sources are stale.
     * 
     * @return
     * @throws MojoExecutionException
     */
    private boolean areSourcesStale() throws MojoExecutionException {
        List<Resource> resources = new ArrayList<Resource>();
        for (String root: (List<String>)project.getCompileSourceRoots()) {
            if (new File(root).exists()) {
                Resource resource = new Resource();
                resource.setDirectory(root);
                resource.addInclude("*.java");
                resources.add(resource);
            }
        }
        return areStale(resources, project.getBuild().getOutputDirectory());
    }

    /**
     * Tests if the project test resources are stale.
     * 
     * @return
     * @throws MojoExecutionException
     */
    private boolean areTestResourcesStale() throws MojoExecutionException {
        return areStale(project.getTestResources(), project.getBuild().getTestOutputDirectory());
    }

    /**
     * Tests if the project test sources are stale.
     * 
     * @return
     * @throws MojoExecutionException
     */
    private boolean areTestSourcesStale() throws MojoExecutionException {
        List<Resource> resources = new ArrayList<Resource>();
        for (String root: (List<String>)project.getTestCompileSourceRoots()) {
            if (new File(root).exists()) { 
                Resource resource = new Resource();
                resource.setDirectory(root);
                resources.add(resource);
            }
        }
        return areStale(resources, project.getBuild().getTestOutputDirectory());
    }

    /**
     * Returns the qualified id of a Maven artifact .
     * @param p a Maven artifact
     * @return a qualified id
     */
    private static String id(Artifact a) {
        return a.getGroupId() + ':' + a.getArtifactId();
    }

    /**
     * Returns the qualified id of a Maven project.
     * @param p a Maven project
     * @return a qualified id
     */
    private static String id(MavenProject p) {
        return p.getGroupId() + ':' + p.getArtifactId();
    }
}