public void execute()

in tools/maven/maven-incremental-build/src/main/java/org/apache/tuscany/sca/tools/incremental/build/plugin/IncrementalBuildMojo.java [92:229]


    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.");
        }
    }