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

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

Introduction

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

Prototype

public Artifact getArtifact() 

Source Link

Usage

From source file:hudson.gridmaven.reporters.MavenArtifactArchiver.java

License:Open Source License

public boolean postBuild(MavenBuildProxy build, MavenProject pom, final BuildListener listener)
        throws InterruptedException, IOException {
    // artifacts that are known to Maven.
    Set<File> mavenArtifacts = new HashSet<File>();

    if (pom.getFile() != null) {// goals like 'clean' runs without loading POM, apparently.
        // record POM
        final MavenArtifact pomArtifact = new MavenArtifact(pom.getGroupId(), pom.getArtifactId(),
                pom.getVersion(), null, "pom", pom.getFile().getName(),
                Util.getDigestOf(new FileInputStream(pom.getFile())));

        final String repositoryUrl = pom.getDistributionManagementArtifactRepository() == null ? null
                : Util.fixEmptyAndTrim(pom.getDistributionManagementArtifactRepository().getUrl());
        final String repositoryId = pom.getDistributionManagementArtifactRepository() == null ? null
                : Util.fixEmptyAndTrim(pom.getDistributionManagementArtifactRepository().getId());

        mavenArtifacts.add(pom.getFile());
        pomArtifact.archive(build, pom.getFile(), listener);

        // record main artifact (if packaging is POM, this doesn't exist)
        final MavenArtifact mainArtifact = MavenArtifact.create(pom.getArtifact());
        if (mainArtifact != null) {
            File f = pom.getArtifact().getFile();
            mavenArtifacts.add(f);/*from w w  w  . java 2 s  . c om*/
            mainArtifact.archive(build, f, listener);
        }

        // record attached artifacts
        final List<MavenArtifact> attachedArtifacts = new ArrayList<MavenArtifact>();
        for (Artifact a : pom.getAttachedArtifacts()) {
            MavenArtifact ma = MavenArtifact.create(a);
            if (ma != null) {
                mavenArtifacts.add(a.getFile());
                ma.archive(build, a.getFile(), listener);
                attachedArtifacts.add(ma);
            }
        }

        // record the action
        build.executeAsync(new MavenBuildProxy.BuildCallable<Void, IOException>() {
            private static final long serialVersionUID = -7955474564875700905L;

            public Void call(MavenBuild build) throws IOException, InterruptedException {
                // if a build forks lifecycles, this method can be called multiple times
                List<MavenArtifactRecord> old = build.getActions(MavenArtifactRecord.class);
                if (!old.isEmpty())
                    build.getActions().removeAll(old);

                MavenArtifactRecord mar = new MavenArtifactRecord(build, pomArtifact, mainArtifact,
                        attachedArtifacts, repositoryUrl, repositoryId);
                build.addAction(mar);

                // TODO kutzi: why are the fingerprints recorded here?
                // I thought that is the job of MavenFingerprinter
                mar.recordFingerprints();

                return null;
            }
        });
    }

    // do we have any assembly artifacts?
    //        System.out.println("Considering "+assemblies+" at "+MavenArtifactArchiver.this);
    //        new Exception().fillInStackTrace().printStackTrace();
    if (assemblies != null) {
        for (File assembly : assemblies) {
            if (mavenArtifacts.contains(assembly))
                continue; // looks like this is already archived
            if (build.isArchivingDisabled()) {
                listener.getLogger().println("[JENKINS] Archiving disabled - not archiving " + assembly);
            } else {
                FilePath target = build.getArtifactsDir().child(assembly.getName());
                listener.getLogger().println("[JENKINS] Archiving " + assembly + " to " + target);
                new FilePath(assembly).copyTo(target);
                // TODO: fingerprint
            }
        }
    }

    return true;
}

From source file:hudson.gridmaven.reporters.MavenFingerprinter.java

License:Open Source License

/**
 * Mojos perform different dependency resolution, so we need to check this for each mojo.
 */// w w  w  .j  a v  a  2s .  c  om
public boolean postExecute(MavenBuildProxy build, MavenProject pom, MojoInfo mojo, BuildListener listener,
        Throwable error) throws InterruptedException, IOException {
    // TODO (kutzi, 2011/09/06): it should be perfectly safe to move all these records to the
    // postBuild method as artifacts should only be added by mojos, but never removed/modified.
    record(pom.getArtifacts(), used);
    record(pom.getArtifact(), produced);
    record(pom.getAttachedArtifacts(), produced);
    record(pom.getGroupId() + ":" + pom.getArtifactId(), pom.getFile(), produced);

    return true;
}

From source file:hudson.gridmaven.reporters.MavenFingerprinter.java

License:Open Source License

private Artifact getArtifact(MavenProject parent) {
    Artifact art = parent.getArtifact();
    if (art == null) {
        // happens for Maven 2.x
        DefaultArtifactHandler artifactHandler = new DefaultArtifactHandler("pom");
        art = new DefaultArtifact(parent.getGroupId(), parent.getArtifactId(),
                VersionRange.createFromVersion(parent.getVersion()), null, "pom", "", artifactHandler);
    }//from w  w  w  . j  a  v  a  2s  . c  om
    return art;
}

From source file:hudson.maven.ReactorReader.java

License:Apache License

/**
 * Tries to resolve the specified artifact from the artifacts of the given project.
 * /*from w  w w .j  ava2 s .co m*/
 * @param project The project to try to resolve the artifact from, must not be <code>null</code>.
 * @param requestedArtifact The artifact to resolve, must not be <code>null</code>.
 * @return The matching artifact from the project or <code>null</code> if not found.
 */
private org.apache.maven.artifact.Artifact findMatchingArtifact(MavenProject project,
        Artifact requestedArtifact) {
    String requestedRepositoryConflictId = getConflictId(requestedArtifact);

    org.apache.maven.artifact.Artifact mainArtifact = project.getArtifact();
    if (requestedRepositoryConflictId.equals(getConflictId(mainArtifact))) {
        mainArtifact.setFile(new File(workspaceRoot, project.getArtifactId()));
        return mainArtifact;
    }

    Collection<org.apache.maven.artifact.Artifact> attachedArtifacts = project.getAttachedArtifacts();
    if (attachedArtifacts != null && !attachedArtifacts.isEmpty()) {
        for (org.apache.maven.artifact.Artifact attachedArtifact : attachedArtifacts) {
            if (requestedRepositoryConflictId.equals(getConflictId(attachedArtifact))) {
                attachedArtifact.setFile(new File(workspaceRoot, project.getArtifactId()));
                return attachedArtifact;
            }
        }
    }

    return null;
}

From source file:hudson.maven.reporters.MavenArtifactArchiver.java

License:Open Source License

public boolean postBuild(MavenBuildProxy build, MavenProject pom, final BuildListener listener)
        throws InterruptedException, IOException {
    // artifacts that are known to Maven.
    Set<File> mavenArtifacts = new HashSet<File>();

    if (pom.getFile() != null) {// goals like 'clean' runs without loading POM, apparently.
        // record POM
        final MavenArtifact pomArtifact = new MavenArtifact(pom.getGroupId(), pom.getArtifactId(),
                pom.getVersion(), null, "pom", pom.getFile().getName(), Util.getDigestOf(pom.getFile()));

        final String repositoryUrl = pom.getDistributionManagementArtifactRepository() == null ? null
                : Util.fixEmptyAndTrim(pom.getDistributionManagementArtifactRepository().getUrl());
        final String repositoryId = pom.getDistributionManagementArtifactRepository() == null ? null
                : Util.fixEmptyAndTrim(pom.getDistributionManagementArtifactRepository().getId());

        mavenArtifacts.add(pom.getFile());
        pomArtifact.archive(build, pom.getFile(), listener);

        // record main artifact (if packaging is POM, this doesn't exist)
        final MavenArtifact mainArtifact = MavenArtifact.create(pom.getArtifact());
        if (mainArtifact != null) {
            File f = pom.getArtifact().getFile();
            mavenArtifacts.add(f);/*  ww w .  jav  a  2 s  .c om*/
            mainArtifact.archive(build, f, listener);
        }

        // record attached artifacts
        final List<MavenArtifact> attachedArtifacts = new ArrayList<MavenArtifact>();
        for (Artifact a : pom.getAttachedArtifacts()) {
            MavenArtifact ma = MavenArtifact.create(a);
            if (ma != null) {
                mavenArtifacts.add(a.getFile());
                ma.archive(build, a.getFile(), listener);
                attachedArtifacts.add(ma);
            }
        }

        // record the action
        build.executeAsync(new MavenBuildProxy.BuildCallable<Void, IOException>() {
            private static final long serialVersionUID = -7955474564875700905L;

            public Void call(MavenBuild build) throws IOException, InterruptedException {
                // if a build forks lifecycles, this method can be called multiple times
                List<MavenArtifactRecord> old = build.getActions(MavenArtifactRecord.class);
                if (!old.isEmpty())
                    build.getActions().removeAll(old);

                MavenArtifactRecord mar = new MavenArtifactRecord(build, pomArtifact, mainArtifact,
                        attachedArtifacts, repositoryUrl, repositoryId);
                build.addAction(mar);

                // TODO kutzi: why are the fingerprints recorded here?
                // I thought that is the job of MavenFingerprinter
                mar.recordFingerprints();

                return null;
            }
        });
    }

    // do we have any assembly artifacts?
    //        System.out.println("Considering "+assemblies+" at "+MavenArtifactArchiver.this);
    //        new Exception().fillInStackTrace().printStackTrace();
    if (build.isArchivingDisabled()) {
        listener.getLogger().println("[JENKINS] Archiving disabled");
    } else if (assemblies != null) {
        for (File assembly : assemblies) {
            if (mavenArtifacts.contains(assembly))
                continue; // looks like this is already archived
            FilePath target = build.getArtifactsDir().child(assembly.getName());
            listener.getLogger().println("[JENKINS] Archiving " + assembly + " to " + target);
            new FilePath(assembly).copyTo(target);
            // TODO: fingerprint
        }
    }

    return true;
}

From source file:hudson.plugins.deploy.MuleApplicationTracker.java

License:Apache License

@Override
public boolean postBuild(final MavenBuildProxy build, final MavenProject project, final BuildListener listener)
        throws InterruptedException, IOException {
    if (project.getArtifact() == null || !"mule".equals(project.getArtifact().getType())) {
        return true;
    }/*from w w  w .  ja  va 2 s . c  o m*/

    if (project.getAttachedArtifacts() != null && !project.getAttachedArtifacts().isEmpty()) {
        //Record the mule application File as an Action
        final String filePath = project.getAttachedArtifacts().get(0).getFile().getPath();
        build.execute(new MavenBuildProxy.BuildCallable<Void, IOException>() {
            @Override
            public Void call(final MavenBuild build) throws IOException, InterruptedException {
                //Mule packaging type attach mule application file first
                build.addAction(new MuleApplicationAction(filePath));
                build.save();
                return null;
            }
        });
    }
    return true;
}

From source file:info.ronjenkins.maven.rtr.steps.BuildSmartReactor.java

License:Apache License

@Override
public void execute(final MavenSession session, final RTRComponents components) {
    // The reactor is a mutable list whose type is unknown, so rather than
    // reassign it via sessions.setProjects(), we will manipulate the live
    // list instead.
    final Iterator<MavenProject> iterator = session.getProjects().iterator();
    MavenProject project;
    while (iterator.hasNext()) {
        project = iterator.next();/*from   www.j a  v a 2  s . c o m*/
        if (!project.getArtifact().isSnapshot()) {
            iterator.remove();
        }
    }
}

From source file:info.ronjenkins.maven.rtr.steps.PerformSmartReactorSanityChecks.java

License:Apache License

@Override
public void execute(final MavenSession session, final RTRComponents components) throws MavenExecutionException {
    // Check for a single POM-only reactor, assuming this is prohibited.
    if (session.getProjects().size() == 1) {
        final MavenProject executionRoot = session.getTopLevelProject();
        if (executionRoot.getArtifact().getType().equals("pom")
                && !RTRConfig.isSinglePomReactorAllowed(session, executionRoot)) {
            this.logger.error("");
            throw new SmartReactorSanityCheckException(
                    "Reactor contains a single POM-packaging project, which is not allowed. If this is intended, set property \""
                            + RTRConfig.PROP_SINGLE_POM_REACTOR_ALLOWED + "\" to true.");
        }//from  www  . ja  v a 2 s . c om
    }
}

From source file:info.ronjenkins.maven.rtr.steps.ValidateSmartReactorEligibility.java

License:Apache License

@Override
public void execute(final MavenSession session, final RTRComponents components) throws MavenExecutionException {
    // Ensure that the Maven Release Plugin is not in the list of goals.
    for (final String goal : session.getGoals()) {
        if (goal.startsWith("release:") || goal.startsWith("org.apache.maven.plugins:maven-release-plugin:")) {
            this.logger.error("");
            throw new SmartReactorSanityCheckException(
                    "A goal from the Maven Release Plugin was specified for execution.");
        }// ww  w. j  a v  a2s.com
    }
    // Ensure that the Maven Release Plugin is not declared in the POM.
    final List<MavenProject> projectsWithMavenReleasePlugin = new ArrayList<>();
    for (final MavenProject project : session.getProjects()) {
        for (final Artifact artifact : project.getPluginArtifacts()) {
            if (artifact.getGroupId().equals("org.apache.maven.plugins")
                    && artifact.getArtifactId().equals("maven-release-plugin")) {
                projectsWithMavenReleasePlugin.add(project);
            }
        }
    }
    if (!projectsWithMavenReleasePlugin.isEmpty()) {
        this.logger.error("");
        for (final MavenProject project : projectsWithMavenReleasePlugin) {
            this.logger.error("Project " + project + " contains a reference to the Maven Release Plugin.");
        }
        this.logger.error("");
        throw new SmartReactorSanityCheckException("Reactor is ineligible to become a Smart Reactor.");
    }
    // Ensure that the root is a SNAPSHOT.
    final MavenProject root = session.getTopLevelProject();
    if (!root.getArtifact().isSnapshot()) {
        this.logger.error("");
        this.logger.error("Top-level project " + root + " is not a SNAPSHOT.");
        this.logger.error("");
        throw new SmartReactorSanityCheckException("Reactor is ineligible to become a Smart Reactor.");
    }
    // Ensure that the ancestors of every SNAPSHOT are also SNAPSHOTs.
    final ProjectDependencyGraph pdg = session.getProjectDependencyGraph();
    final List<MavenProject> badProjects = new ArrayList<>();
    for (final MavenProject project : session.getProjects()) {
        if (project.getArtifact().isSnapshot()) {
            for (final MavenProject ancestor : pdg.getUpstreamProjects(project, true)) {
                if (!ancestor.getArtifact().isSnapshot()) {
                    badProjects.add(ancestor);
                }
            }
        }
    }
    // Fail if necessary.
    if (!badProjects.isEmpty()) {
        this.logger.error("");
        this.logger.error(
                "The following release projects in the reactor have SNAPSHOT dependencies in the reactor, which is not allowed:");
        for (final MavenProject badProject : badProjects) {
            this.logger.error("  " + badProject.getArtifact().toString() + " @ "
                    + badProject.getFile().getAbsolutePath());
        }
        this.logger.error("");
        throw new SmartReactorSanityCheckException("Reactor is ineligible to become a Smart Reactor.");
    }
}

From source file:io.airlift.resolver.ArtifactResolver.java

License:Apache License

public List<Artifact> resolvePom(File pomFile) {
    if (pomFile == null) {
        throw new RuntimeException("pomFile is null");
    }/*w w w.  j av  a 2  s  .c  o m*/

    MavenProject pom;
    try {
        PlexusContainer container = container();
        org.apache.maven.repository.RepositorySystem lrs = container
                .lookup(org.apache.maven.repository.RepositorySystem.class);
        ProjectBuilder projectBuilder = container.lookup(ProjectBuilder.class);
        ProjectBuildingRequest request = new DefaultProjectBuildingRequest();
        request.setSystemProperties(requiredSystemProperties());
        request.setRepositorySession(repositorySystemSession);
        request.setProcessPlugins(false);
        request.setLocalRepository(lrs.createDefaultLocalRepository());
        request.setRemoteRepositories(
                Arrays.asList(new ArtifactRepository[] { lrs.createDefaultRemoteRepository() }.clone()));
        ProjectBuildingResult result = projectBuilder.build(pomFile, request);
        pom = result.getProject();
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw new RuntimeException("Error loading pom: " + pomFile.getAbsolutePath(), e);
    }

    Artifact rootArtifact = new DefaultArtifact(pom.getArtifact().getGroupId(),
            pom.getArtifact().getArtifactId(), pom.getArtifact().getClassifier(), pom.getArtifact().getType(),
            pom.getArtifact().getVersion(), null, new File(pom.getModel().getBuild().getOutputDirectory()));

    CollectRequest collectRequest = new CollectRequest();
    for (org.apache.maven.model.Dependency dependency : pom.getDependencies()) {
        collectRequest.addDependency(toAetherDependency(dependency));
    }
    for (RemoteRepository repository : pom.getRemoteProjectRepositories()) {
        collectRequest.addRepository(repository);
    }
    for (RemoteRepository repository : repositories) {
        collectRequest.addRepository(repository);
    }

    // Make sure we account for managed dependencies
    if (pom.getDependencyManagement() != null) {
        for (org.apache.maven.model.Dependency managedDependency : pom.getDependencyManagement()
                .getDependencies()) {
            collectRequest.addManagedDependency(toAetherDependency(managedDependency));
        }
    }

    DependencyRequest dependencyRequest = new DependencyRequest(collectRequest,
            DependencyFilterUtils.classpathFilter(JavaScopes.RUNTIME));
    List<Artifact> artifacts = resolveArtifacts(dependencyRequest);
    return ImmutableList.<Artifact>builder().add(rootArtifact).addAll(artifacts).build();
}