Example usage for org.apache.maven.project MavenProject hasLifecyclePhase

List of usage examples for org.apache.maven.project MavenProject hasLifecyclePhase

Introduction

In this page you can find the example usage for org.apache.maven.project MavenProject hasLifecyclePhase.

Prototype

public boolean hasLifecyclePhase(String phase) 

Source Link

Document

Warning: This is an internal utility method that is only public for technical reasons, it is not part of the public API.

Usage

From source file:io.fabric8.maven.CreateProfileZipMojo.java

License:Apache License

protected void generateAggregatedZip(MavenProject rootProject, List<MavenProject> reactorProjects,
        List<MavenProject> pomZipProjects) throws IOException, MojoExecutionException {
    File projectBaseDir = rootProject.getBasedir();
    File projectOutputFile = new File(projectBaseDir, "target/profile.zip");
    getLog().info("Generating " + projectOutputFile.getAbsolutePath() + " from root project "
            + rootProject.getArtifactId());
    File projectBuildDir = new File(projectBaseDir, reactorProjectOutputPath);

    createAggregatedZip(reactorProjects, projectBaseDir, projectBuildDir, reactorProjectOutputPath,
            projectOutputFile, includeReadMe, pomZipProjects);
    if (rootProject.getAttachedArtifacts() != null) {
        // need to remove existing as otherwise we get a WARN
        Artifact found = null;/*  ww  w .  ja v  a  2  s  .c  o m*/
        for (Artifact artifact : rootProject.getAttachedArtifacts()) {
            if (artifactClassifier != null && artifact.hasClassifier()
                    && artifact.getClassifier().equals(artifactClassifier)) {
                found = artifact;
                break;
            }
        }
        if (found != null) {
            rootProject.getAttachedArtifacts().remove(found);
        }
    }

    getLog().info("Attaching aggregated zip " + projectOutputFile + " to root project "
            + rootProject.getArtifactId());
    projectHelper.attachArtifact(rootProject, artifactType, artifactClassifier, projectOutputFile);

    // if we are doing an install goal, then also install the aggregated zip manually
    // as maven will install the root project first, and then build the reactor projects, and at this point
    // it does not help to attach artifact to root project, as those artifacts will not be installed
    // so we need to install manually
    if (rootProject.hasLifecyclePhase("install")) {
        getLog().info("Installing aggregated zip " + projectOutputFile);
        InvocationRequest request = new DefaultInvocationRequest();
        request.setBaseDirectory(rootProject.getBasedir());
        request.setPomFile(new File("./pom.xml"));
        request.setGoals(Collections.singletonList("install:install-file"));
        request.setRecursive(false);
        request.setInteractive(false);

        Properties props = new Properties();
        props.setProperty("file", "target/profile.zip");
        props.setProperty("groupId", rootProject.getGroupId());
        props.setProperty("artifactId", rootProject.getArtifactId());
        props.setProperty("version", rootProject.getVersion());
        props.setProperty("classifier", "profile");
        props.setProperty("packaging", "zip");
        request.setProperties(props);

        getLog().info(
                "Installing aggregated zip using: mvn install:install-file" + serializeMvnProperties(props));
        Invoker invoker = new DefaultInvoker();
        try {
            InvocationResult result = invoker.execute(request);
            if (result.getExitCode() != 0) {
                throw new IllegalStateException("Error invoking Maven goal install:install-file");
            }
        } catch (MavenInvocationException e) {
            throw new MojoExecutionException("Error invoking Maven goal install:install-file", e);
        }
    }
}

From source file:io.fabric8.maven.ZipMojo.java

License:Apache License

protected void generateAggregatedZip(MavenProject rootProject, List<MavenProject> reactorProjects,
        Set<MavenProject> pomZipProjects) throws IOException, MojoExecutionException {
    File projectBaseDir = rootProject.getBasedir();
    String rootProjectGroupId = rootProject.getGroupId();
    String rootProjectArtifactId = rootProject.getArtifactId();
    String rootProjectVersion = rootProject.getVersion();

    String aggregatedZipFileName = "target/" + rootProjectArtifactId + "-" + rootProjectVersion + "-app.zip";
    File projectOutputFile = new File(projectBaseDir, aggregatedZipFileName);
    getLog().info("Generating " + projectOutputFile.getAbsolutePath() + " from root project "
            + rootProjectArtifactId);/*from www  . jav a2 s  .  c  om*/
    File projectBuildDir = new File(projectBaseDir, reactorProjectOutputPath);

    if (projectOutputFile.exists()) {
        projectOutputFile.delete();
    }
    createAggregatedZip(projectBaseDir, projectBuildDir, reactorProjectOutputPath, projectOutputFile,
            includeReadMe, pomZipProjects);
    if (rootProject.getAttachedArtifacts() != null) {
        // need to remove existing as otherwise we get a WARN
        Artifact found = null;
        for (Artifact artifact : rootProject.getAttachedArtifacts()) {
            if (artifactClassifier != null && artifact.hasClassifier()
                    && artifact.getClassifier().equals(artifactClassifier)) {
                found = artifact;
                break;
            }
        }
        if (found != null) {
            rootProject.getAttachedArtifacts().remove(found);
        }
    }

    getLog().info("Attaching aggregated zip " + projectOutputFile + " to root project "
            + rootProject.getArtifactId());
    projectHelper.attachArtifact(rootProject, artifactType, artifactClassifier, projectOutputFile);

    // if we are doing an install goal, then also install the aggregated zip manually
    // as maven will install the root project first, and then build the reactor projects, and at this point
    // it does not help to attach artifact to root project, as those artifacts will not be installed
    // so we need to install manually
    List<String> activeProfileIds = new ArrayList<>();
    List<Profile> activeProfiles = rootProject.getActiveProfiles();
    if (activeProfiles != null) {
        for (Profile profile : activeProfiles) {
            String id = profile.getId();
            if (Strings.isNotBlank(id)) {
                activeProfileIds.add(id);
            }
        }
    }
    if (rootProject.hasLifecyclePhase("install")) {
        getLog().info("Installing aggregated zip " + projectOutputFile);
        InvocationRequest request = new DefaultInvocationRequest();
        request.setBaseDirectory(rootProject.getBasedir());
        request.setPomFile(new File("./pom.xml"));
        request.setGoals(Collections.singletonList("install:install-file"));
        request.setRecursive(false);
        request.setInteractive(false);
        request.setProfiles(activeProfileIds);

        Properties props = new Properties();
        props.setProperty("file", aggregatedZipFileName);
        props.setProperty("groupId", rootProjectGroupId);
        props.setProperty("artifactId", rootProjectArtifactId);
        props.setProperty("version", rootProjectVersion);
        props.setProperty("classifier", "app");
        props.setProperty("packaging", "zip");
        props.setProperty("generatePom", "false");
        request.setProperties(props);

        getLog().info(
                "Installing aggregated zip using: mvn install:install-file" + serializeMvnProperties(props));
        Invoker invoker = new DefaultInvoker();
        try {
            InvocationResult result = invoker.execute(request);
            if (result.getExitCode() != 0) {
                throw new IllegalStateException("Error invoking Maven goal install:install-file");
            }
        } catch (MavenInvocationException e) {
            throw new MojoExecutionException("Error invoking Maven goal install:install-file", e);
        }
    }

    if (rootProject.hasLifecyclePhase("deploy")) {

        if (deploymentRepository == null && Strings.isNullOrBlank(altDeploymentRepository)) {
            String msg = "Cannot run deploy phase as Maven project has no <distributionManagement> with the maven url to use for deploying the aggregated zip file, neither an altDeploymentRepository property.";
            getLog().warn(msg);
            throw new MojoExecutionException(msg);
        }

        getLog().info("Deploying aggregated zip " + projectOutputFile + " to root project "
                + rootProject.getArtifactId());
        getLog().info("Using deploy goal: " + deployFileGoal + " with active profiles: " + activeProfileIds);

        InvocationRequest request = new DefaultInvocationRequest();
        request.setBaseDirectory(rootProject.getBasedir());
        request.setPomFile(new File("./pom.xml"));
        request.setGoals(Collections.singletonList(deployFileGoal));
        request.setRecursive(false);
        request.setInteractive(false);
        request.setProfiles(activeProfileIds);
        request.setProperties(getProjectAndFabric8Properties(getProject()));

        Properties props = new Properties();
        props.setProperty("file", aggregatedZipFileName);
        props.setProperty("groupId", rootProjectGroupId);
        props.setProperty("artifactId", rootProjectArtifactId);
        props.setProperty("version", rootProjectVersion);
        props.setProperty("classifier", "app");
        props.setProperty("packaging", "zip");
        String deployUrl = null;

        if (!Strings.isNullOrBlank(deployFileUrl)) {
            deployUrl = deployFileUrl;
        } else if (altDeploymentRepository != null && altDeploymentRepository.contains("::")) {
            deployUrl = altDeploymentRepository.substring(altDeploymentRepository.lastIndexOf("::") + 2);
        } else {
            deployUrl = deploymentRepository.getUrl();
        }

        props.setProperty("url", deployUrl);
        props.setProperty("repositoryId", deploymentRepository.getId());
        props.setProperty("generatePom", "false");
        request.setProperties(props);

        getLog().info("Deploying aggregated zip using: mvn deploy:deploy-file" + serializeMvnProperties(props));
        Invoker invoker = new DefaultInvoker();
        try {
            InvocationResult result = invoker.execute(request);
            if (result.getExitCode() != 0) {
                throw new IllegalStateException("Error invoking Maven goal deploy:deploy-file");
            }
        } catch (MavenInvocationException e) {
            throw new MojoExecutionException("Error invoking Maven goal deploy:deploy-file", e);
        }
    }
}

From source file:io.takari.maven.workspace.GenerationsWorkspaceReader.java

License:Apache License

private File find(MavenProject project, Artifact artifact) {
    File file = null;//from ww w .ja v a2s .c om

    if ("pom".equals(artifact.getExtension())) {
        return project.getFile();
    }

    //
    // project = project where we will find artifact, this may be the primary artifact or any of the 
    // secondary artifacts
    //
    Artifact projectArtifact = findMatchingArtifact(project, artifact);

    if (hasArtifactFileFromPackagePhase(projectArtifact)) {
        //
        // We have gone far enough in the lifecycle to produce a JAR, WAR, or other file-based artifact
        //
        if (isTestArtifact(artifact)) {
            //
            // We are looking for a test JAR foo-1.0-test.jar
            //
            file = new File(project.getBuild().getDirectory(),
                    String.format("%s-%s-tests.jar", project.getArtifactId(), project.getVersion()));
        } else {
            //
            // We are looking for an application JAR foo-1.0.jar
            //
            file = projectArtifact.getFile();
        }
    } else if (!hasBeenPackaged(project)) {
        //
        // Here no file has been produced so we fallback to loose class files only if artifacts haven't been packaged yet
        // and only for plain old jars. Not war files, not ear files, not anything else.
        //                    
        String type = artifact.getProperty("type", "");

        if (isTestArtifact(artifact)) {
            if (project.hasLifecyclePhase("test-compile")) {
                file = new File(project.getBuild().getTestOutputDirectory());
            }
        } else if (project.hasLifecyclePhase("compile") && COMPILE_PHASE_TYPES.contains(type)) {
            file = new File(project.getBuild().getOutputDirectory());
        }
        if (file == null && allowArtifactsWithoutAFileToBeResolvedInTheReactor) {
            //
            // There is no elegant way to signal that the Artifact's representation is actually present in the 
            // reactor but that it has no file-based representation during this execution and may, in fact, not
            // require one. The case this accounts for something I am doing with Eclipse project file
            // generation where I can perform dependency resolution, but cannot run any lifecycle phases.
            // I need the in-reactor references to work correctly. The WorkspaceReader interface needs
            // to be changed to account for this. This is not exactly elegant, but it's turned on
            // with a property and should not interfere.
            //
            // We have made a slight change in that we don't want to return "." because it confuses the 
            // compiler plugin for special cases that we have where we are running the compiler in the
            // process-resources phase.
            //
            file = new File(project.getBuild().getOutputDirectory());
            //if (file.exists() == false) {
            //  file = new File(".");
            //}
        }
    }

    //
    // The fall-through indicates that the artifact cannot be found;
    // for instance if package produced nothing or classifier problems.
    //
    return file;
}

From source file:io.takari.maven.workspace.GenerationsWorkspaceReader.java

License:Apache License

private File findWorkspaceArtifact(MavenProject project, Artifact artifact) {
    File file = null;//  w  w  w  .j a v  a2s  .  c  om

    if ("pom".equals(artifact.getExtension())) {
        return project.getFile();
    }

    //
    // project = project where we will find artifact, this may be the primary artifact or any of the 
    // secondary artifacts
    //
    Artifact projectArtifact = findMatchingArtifact(project, artifact);

    if (hasArtifactFileFromPackagePhase(projectArtifact)) {
        //
        // We have gone far enough in the lifecycle to produce a JAR, WAR, or other file-based artifact
        //
        if (isTestArtifact(artifact)) {
            //
            // We are looking for a test JAR foo-1.0-test.jar
            //
            file = new File(project.getBuild().getDirectory(),
                    String.format("%s-%s-tests.jar", project.getArtifactId(), project.getVersion()));
        } else {
            //
            // We are looking for an application JAR foo-1.0.jar
            //
            file = projectArtifact.getFile();
        }
    } else if (!hasBeenPackaged(project)) {
        //
        // Here no file has been produced so we fallback to loose class files only if artifacts haven't been packaged yet
        // and only for plain old jars. Not war files, not ear files, not anything else.
        //                    
        String type = artifact.getProperty("type", "");

        if (isTestArtifact(artifact)) {
            if (project.hasLifecyclePhase("test-compile")) {
                file = new File(project.getBuild().getTestOutputDirectory());
            }
        } else if (project.hasLifecyclePhase("compile") && COMPILE_PHASE_TYPES.contains(type)) {
            file = new File(project.getBuild().getOutputDirectory());
        }
        if (file == null && allowArtifactsWithoutAFileToBeResolvedInTheReactor) {
            //
            // There is no elegant way to signal that the Artifact's representation is actually present in the 
            // reactor but that it has no file-based representation during this execution and may, in fact, not
            // require one. The case this accounts for something I am doing with Eclipse project file
            // generation where I can perform dependency resolution, but cannot run any lifecycle phases.
            // I need the in-reactor references to work correctly. The WorkspaceReader interface needs
            // to be changed to account for this. This is not exactly elegant, but it's turned on
            // with a property and should not interfere.
            // 

            /*
                    
            Turn off resolving for now
                    
            file = new File(project.getBuild().getOutputDirectory());
            if (file.exists() == false) {
              file = new File(".");
            }
                    
            */
        }
    }

    //
    // The fall-through indicates that the artifact cannot be found;
    // for instance if package produced nothing or classifier problems.
    //
    return file;
}

From source file:io.takari.maven.workspace.GenerationsWorkspaceReader.java

License:Apache License

private boolean hasBeenPackaged(MavenProject project) {
    return project.hasLifecyclePhase("package") || project.hasLifecyclePhase("install")
            || project.hasLifecyclePhase("deploy");
}

From source file:org.sourcepit.maven.bootstrap.internal.core.ReactorReader.java

License:Apache License

private File find(MavenProject project, Artifact artifact) {
    if ("pom".equals(artifact.getExtension())) {
        return project.getFile();
    }//from  w  ww .  j av  a2  s  .com

    org.apache.maven.artifact.Artifact projectArtifact = findMatchingArtifact(project, artifact);

    if (hasArtifactFileFromPackagePhase(projectArtifact)) {
        return projectArtifact.getFile();
    } else if (!hasBeenPackaged(project)) {
        // fallback to loose class files only if artifacts haven't been packaged yet

        if (isTestArtifact(artifact)) {
            if (project.hasLifecyclePhase("test-compile")) {
                return new File(project.getBuild().getTestOutputDirectory());
            }
        } else {
            if (project.hasLifecyclePhase("compile")) {
                return new File(project.getBuild().getOutputDirectory());
            }
        }
    }

    // The fall-through indicates that the artifact cannot be found;
    // for instance if package produced nothing or classifier problems.
    return null;
}