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

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

Introduction

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

Prototype

public void setArtifact(Artifact artifact) 

Source Link

Usage

From source file:com.webcohesion.enunciate.mojo.DeployArtifactBaseMojo.java

License:Apache License

public void execute() throws MojoExecutionException, MojoFailureException {
    if (this.enunciateArtifactId == null) {
        throw new MojoExecutionException("An enunciate artifact id must be supplied.");
    }/*from   w w w  .  jav  a  2s.  c o m*/

    Enunciate enunciate = (Enunciate) getPluginContext().get(ConfigMojo.ENUNCIATE_PROPERTY);
    if (enunciate == null) {
        throw new MojoExecutionException("No enunciate mechanism found in the project!");
    }

    com.webcohesion.enunciate.artifacts.Artifact enunciateArtifact = enunciate
            .findArtifact(this.enunciateArtifactId);
    if (enunciateArtifact == null) {
        throw new MojoExecutionException("Unknown Enunciate artifact: " + this.enunciateArtifactId + ".");
    }

    File mainArtifact = null;
    File sources = null;
    File javadocs = null;
    if (enunciateArtifact instanceof ClientLibraryArtifact) {
        for (com.webcohesion.enunciate.artifacts.Artifact childArtifact : ((ClientLibraryArtifact) enunciateArtifact)
                .getArtifacts()) {
            if (childArtifact instanceof FileArtifact) {
                ArtifactType artifactType = ((FileArtifact) childArtifact).getArtifactType();
                if (artifactType != null) {
                    switch (artifactType) {
                    case binaries:
                        mainArtifact = ((FileArtifact) childArtifact).getFile();
                        break;
                    case sources:
                        sources = ((FileArtifact) childArtifact).getFile();
                        break;
                    case javadocs:
                        javadocs = ((FileArtifact) childArtifact).getFile();
                        break;
                    }
                }
            }
        }
    } else if (enunciateArtifact instanceof FileArtifact) {
        mainArtifact = ((FileArtifact) enunciateArtifact).getFile();
    } else {
        try {
            mainArtifact = enunciate.createTempFile(this.enunciateArtifactId, "artifact");
            enunciateArtifact.exportTo(mainArtifact, enunciate);
        } catch (IOException e) {
            throw new MojoExecutionException("Unable to create a temp file.", e);
        }
    }

    if (mainArtifact == null) {
        if (sources != null) {
            mainArtifact = sources;
            sources = null;
        }
    }

    if (mainArtifact == null) {
        throw new MojoExecutionException(
                "Unable to determine the file to deploy from enunciate artifact " + enunciateArtifactId + ".");
    }

    // Process the supplied POM (if there is one)
    Model model = null;
    if (pomFile != null) {
        generatePom = false;
        model = readModel(pomFile);
        processModel(model);
    }

    if (this.packaging == null) {
        String artifactName = mainArtifact.getName();
        int dotIndex = artifactName.indexOf('.');
        if (dotIndex > 0 && (dotIndex + 1 < artifactName.length())) {
            this.packaging = artifactName.substring(dotIndex + 1);
        }
    }

    if (this.packaging == null) {
        throw new MojoExecutionException("Unable to determine the packaging of enunciate artifact "
                + enunciateArtifactId + ". Please specify it in the configuration.");
    }

    if (this.version == null) {
        throw new MojoExecutionException("Null version.");
    }

    if (model == null) {
        model = new Model();
        model.setModelVersion("4.0.0");
        model.setGroupId(this.groupId);
        model.setArtifactId(this.artifactId);
        model.setVersion(this.version);
        model.setPackaging(this.packaging);
        model.setDescription(this.description);
    }

    ArtifactRepository repo = getDeploymentRepository();

    String protocol = repo.getProtocol();

    if (protocol.equals("scp")) {
        File sshFile = new File(System.getProperty("user.home"), ".ssh");

        if (!sshFile.exists()) {
            sshFile.mkdirs();
        }
    }

    Artifact artifact = this.artifactFactory.createArtifactWithClassifier(groupId, artifactId, version,
            packaging, classifier);

    // Upload the POM if requested, generating one if need be
    if (generatePom) {
        ArtifactMetadata metadata = new ProjectArtifactMetadata(artifact, generatePomFile(model));
        artifact.addMetadata(metadata);
    } else {
        ArtifactMetadata metadata = new ProjectArtifactMetadata(artifact, pomFile);
        artifact.addMetadata(metadata);
    }

    try {
        getDeployer().deploy(mainArtifact, artifact, repo, this.localRepository);

        if (sources != null || javadocs != null) {
            MavenProject project = new MavenProject(model);
            project.setArtifact(artifact);
            if (sources != null) {
                //we have to do it this way because of classloading issues.
                this.projectHelper.attachArtifact(project, artifact.getType(), "sources", sources);
                getDeployer().deploy(sources, (Artifact) project.getAttachedArtifacts().get(0), repo,
                        this.localRepository);
            }

            if (javadocs != null) {
                //we have to do it this way because of classloading issues.
                this.projectHelper.attachArtifact(project, artifact.getType(), "javadoc", javadocs);
                getDeployer().deploy(javadocs, (Artifact) project.getAttachedArtifacts().get(0), repo,
                        this.localRepository);
            }
        }
    } catch (ArtifactDeploymentException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}

From source file:de.eacg.ecs.plugin.ProjectFix.java

public static void fixProject(MavenProject model) {
    Artifact a = model.getArtifact();/* w  ww.  j av a  2 s. c  om*/
    String str = String.format("%s:%s:%s:%s:%s", a.getGroupId(), a.getArtifactId(), a.getType(),
            a.hasClassifier() ? a.getClassifier() : "", a.getVersion());

    String fixed = lookup.get(str);
    if (fixed != null) {
        String[] parts = fixed.split(":");
        if (parts.length == 5) {
            model.setGroupId(parts[0]);
            model.setArtifactId(parts[1]);
            model.setPackaging(parts[2]);
            model.setVersion(parts[4]);
            model.setArtifact(new DefaultArtifact(parts[0], parts[1], parts[4], model.getArtifact().getScope(),
                    parts[2], parts[3].isEmpty() ? null : parts[3], model.getArtifact().getArtifactHandler()));
        }
    }
}

From source file:io.sundr.maven.AbstractSundrioMojo.java

License:Apache License

MavenProject readProject(File pomFile) throws IOException {
    MavenXpp3Reader mavenReader = new MavenXpp3Reader();
    FileReader fileReader = null;
    try {/*from  ww  w.  j a v a 2 s  .c  o  m*/
        fileReader = new FileReader(pomFile);
        Model model = mavenReader.read(fileReader);
        model.setPomFile(pomFile);
        MavenProject project = new MavenProject(model);
        project.setFile(pomFile);
        project.setArtifact(createArtifact(pomFile, model.getGroupId(), model.getArtifactId(),
                model.getVersion(), "compile", model.getPackaging(), ""));
        return project;
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (fileReader != null) {
            fileReader.close();
        }
    }
}

From source file:io.sundr.maven.GenerateBomMojo.java

License:Apache License

/**
 * Returns the generated {@link org.apache.maven.project.MavenProject} to build.
 * This version of the project contains all the stuff needed for building (parents, profiles, properties etc).
 *
 * @param project The source {@link org.apache.maven.project.MavenProject}.
 * @param config  The {@link io.sundr.maven.BomConfig}.
 * @return The build {@link org.apache.maven.project.MavenProject}.
 *//*from   w w  w .  j a  v  a 2  s  .co  m*/
private static MavenProject toBuild(MavenProject project, BomConfig config) {
    File outputDir = new File(project.getBuild().getOutputDirectory());
    File bomDir = new File(outputDir, config.getArtifactId());
    File generatedBom = new File(bomDir, BOM_NAME);

    MavenProject toBuild = project.clone();
    //we want to avoid recursive "generate-bom".
    toBuild.setExecutionRoot(false);
    toBuild.setFile(generatedBom);
    toBuild.getModel().setPomFile(generatedBom);
    toBuild.setModelVersion(project.getModelVersion());

    toBuild.setArtifact(new DefaultArtifact(project.getGroupId(), config.getArtifactId(), project.getVersion(),
            project.getArtifact().getScope(), project.getArtifact().getType(),
            project.getArtifact().getClassifier(), project.getArtifact().getArtifactHandler()));

    toBuild.setParent(project.getParent());
    toBuild.getModel().setParent(project.getModel().getParent());

    toBuild.setGroupId(project.getGroupId());
    toBuild.setArtifactId(config.getArtifactId());
    toBuild.setVersion(project.getVersion());
    toBuild.setPackaging("pom");
    toBuild.setName(config.getName());
    toBuild.setDescription(config.getDescription());

    toBuild.setUrl(project.getUrl());
    toBuild.setLicenses(project.getLicenses());
    toBuild.setScm(project.getScm());
    toBuild.setDevelopers(project.getDevelopers());
    toBuild.setDistributionManagement(project.getDistributionManagement());
    toBuild.getModel().setProfiles(project.getModel().getProfiles());

    //We want to avoid having the generated stuff wiped.
    toBuild.getProperties().put("clean.skip", "true");
    toBuild.getModel().getBuild().setDirectory(bomDir.getAbsolutePath());
    toBuild.getModel().getBuild().setOutputDirectory(new File(bomDir, "target").getAbsolutePath());
    for (String key : config.getProperties().stringPropertyNames()) {
        toBuild.getProperties().put(key, config.getProperties().getProperty(key));
    }
    return toBuild;
}

From source file:org.apache.felix.bundleplugin.BundleAllPlugin.java

License:Apache License

/**
 * Bundle a project and its transitive dependencies up to some depth level
 * /* www  .  j a va 2  s  .  c  o  m*/
 * @param project
 * @param maxDepth how deep to process the dependency tree
 * @throws MojoExecutionException
 */
protected BundleInfo bundleAll(MavenProject project, int maxDepth) throws MojoExecutionException {

    if (alreadyBundled(project.getArtifact())) {
        getLog().debug("Ignoring project already processed " + project.getArtifact());
        return null;
    }

    if (m_artifactsBeingProcessed.contains(project.getArtifact())) {
        getLog().warn("Ignoring artifact due to dependency cycle " + project.getArtifact());
        return null;
    }
    m_artifactsBeingProcessed.add(project.getArtifact());

    DependencyNode dependencyTree;

    try {
        dependencyTree = m_dependencyTreeBuilder.buildDependencyTree(project, localRepository, m_factory,
                m_artifactMetadataSource, null, m_collector);
    } catch (DependencyTreeBuilderException e) {
        throw new MojoExecutionException("Unable to build dependency tree", e);
    }

    BundleInfo bundleInfo = new BundleInfo();

    if (!dependencyTree.hasChildren()) {
        /* no need to traverse the tree */
        return bundleRoot(project, bundleInfo);
    }

    getLog().debug("Will bundle the following dependency tree" + LS + dependencyTree);

    for (Iterator it = dependencyTree.inverseIterator(); it.hasNext();) {
        DependencyNode node = (DependencyNode) it.next();
        if (!it.hasNext()) {
            /* this is the root, current project */
            break;
        }

        if (node.getState() != DependencyNode.INCLUDED) {
            continue;
        }

        if (Artifact.SCOPE_SYSTEM.equals(node.getArtifact().getScope())) {
            getLog().debug("Ignoring system scoped artifact " + node.getArtifact());
            continue;
        }

        Artifact artifact;
        try {
            artifact = resolveArtifact(node.getArtifact());
        } catch (ArtifactNotFoundException e) {
            if (ignoreMissingArtifacts) {
                continue;
            }

            throw new MojoExecutionException("Artifact was not found in the repo" + node.getArtifact(), e);
        }

        node.getArtifact().setFile(artifact.getFile());

        int nodeDepth = node.getDepth();
        if (nodeDepth > maxDepth) {
            /* node is deeper than we want */
            getLog().debug(
                    "Ignoring " + node.getArtifact() + ", depth is " + nodeDepth + ", bigger than " + maxDepth);
            continue;
        }

        MavenProject childProject;
        try {
            childProject = m_mavenProjectBuilder.buildFromRepository(artifact, remoteRepositories,
                    localRepository, true);
            if (childProject.getDependencyArtifacts() == null) {
                childProject.setDependencyArtifacts(childProject.createArtifacts(m_factory, null, null));
            }
        } catch (ProjectBuildingException e) {
            throw new MojoExecutionException("Unable to build project object for artifact " + artifact, e);
        } catch (InvalidDependencyVersionException e) {
            throw new MojoExecutionException("Invalid dependency version for artifact " + artifact);
        }

        childProject.setArtifact(artifact);
        getLog().debug("Child project artifact location: " + childProject.getArtifact().getFile());

        if ((Artifact.SCOPE_COMPILE.equals(artifact.getScope()))
                || (Artifact.SCOPE_RUNTIME.equals(artifact.getScope()))) {
            BundleInfo subBundleInfo = bundleAll(childProject, maxDepth - 1);
            if (subBundleInfo != null) {
                bundleInfo.merge(subBundleInfo);
            }
        } else {
            getLog().debug("Not processing due to scope (" + childProject.getArtifact().getScope() + "): "
                    + childProject.getArtifact());
        }
    }

    return bundleRoot(project, bundleInfo);
}

From source file:org.apache.felix.obr.plugin.AbstractFileMojo.java

License:Apache License

/**
 * @return project based on command-line settings, with bundle attached
 * @throws MojoExecutionException//ww w  .j  a va  2  s .  c  o  m
 */
public MavenProject getProject() throws MojoExecutionException {
    final MavenProject project;
    if (pomFile != null && pomFile.exists()) {
        project = PomHelper.readPom(pomFile);

        groupId = project.getGroupId();
        artifactId = project.getArtifactId();
        version = project.getVersion();
        packaging = project.getPackaging();
    } else {
        project = PomHelper.buildPom(groupId, artifactId, version, packaging);
    }

    if (groupId == null || artifactId == null || version == null || packaging == null) {
        throw new MojoExecutionException("Missing group, artifact, version, or packaging information");
    }

    Artifact bundle = m_factory.createArtifactWithClassifier(groupId, artifactId, version, packaging,
            classifier);
    project.setArtifact(bundle);

    return project;
}

From source file:org.apache.hyracks.maven.license.LicenseMojo.java

License:Apache License

private void gatherProjectDependencies(MavenProject project,
        Map<MavenProject, List<Pair<String, String>>> dependencyLicenseMap,
        Map<String, MavenProject> dependencyGavMap) throws ProjectBuildingException {
    final Set dependencyArtifacts = project.getArtifacts();
    if (dependencyArtifacts != null) {
        for (Object depArtifactObj : dependencyArtifacts) {
            final Artifact depArtifact = (Artifact) depArtifactObj;
            if (!excludedScopes.contains(depArtifact.getScope())) {
                MavenProject dep = resolveDependency(depArtifact);
                dep.setArtifact(depArtifact);
                dependencyGavMap.put(toGav(dep), dep);
                List<Pair<String, String>> licenseUrls = new ArrayList<>();
                for (Object license : dep.getLicenses()) {
                    final License license1 = (License) license;
                    String url = license1.getUrl() != null ? license1.getUrl()
                            : (license1.getName() != null ? license1.getName() : "LICENSE_EMPTY_NAME_URL");
                    licenseUrls.add(new ImmutablePair<>(url, license1.getName()));
                }/*from   w w w  . ja  v a 2  s.c om*/
                dependencyLicenseMap.put(dep, licenseUrls);
            }
        }
    }
}

From source file:org.apache.hyracks.maven.license.LicenseMojo.java

License:Apache License

protected MavenProject resolveDependency(Artifact depObj) throws ProjectBuildingException {
    String key = depObj.getGroupId() + ":" + depObj.getArtifactId() + ":" + depObj.getVersion();

    MavenProject depProj = projectCache.get(key);

    if (depProj == null) {
        try {/*from w ww . j  a  v  a  2  s .c  o m*/
            depProj = projectBuilder.buildFromRepository(depObj, remoteRepositories, localRepository, false);
        } catch (ProjectBuildingException e) {
            throw new ProjectBuildingException(key, "Error creating dependent artifacts", e);
        }

        Model supplement = supplementModels.get(
                SupplementalModelHelper.generateSupplementMapKey(depObj.getGroupId(), depObj.getArtifactId()));
        if (supplement != null) {
            Model merged = SupplementalModelHelper.mergeModels(assembler, depProj.getModel(), supplement);
            Set<String> origLicenses = depProj.getModel().getLicenses().stream().map(License::getUrl)
                    .collect(Collectors.toSet());
            Set<String> newLicenses = merged.getLicenses().stream().map(License::getUrl)
                    .collect(Collectors.toSet());
            if (!origLicenses.equals(newLicenses)) {
                getLog().warn("license list for " + toGav(depProj) + " changed with supplemental model; was: "
                        + origLicenses + ", now: " + newLicenses);
            }
            depProj = new MavenProject(merged);
            depProj.setArtifact(depObj);
            depProj.setVersion(depObj.getVersion());
        }
        depProj.getArtifact().setScope(depObj.getScope());
        projectCache.put(key, depProj);
    }
    return depProj;
}

From source file:org.apache.karaf.tooling.semantic.UnitHelp.java

License:Apache License

/**
 * Resolve maven URI into maven project.
 * <p>//from  w  ww .j a va  2 s. c  o m
 * Provides only model and artifact.
 */
public static MavenProject newProject(String mavenURI) throws Exception {

    final Artifact artifact = newArtifact(mavenURI);

    final File input = artifact.getFile();

    final ModelReader reader = new DefaultModelReader();

    final Model model = reader.read(input, null);

    final MavenProject project = new MavenProject(model);

    project.setArtifact(RepositoryUtils.toArtifact(artifact));

    return project;

}

From source file:org.debian.dependency.sources.SCMSourceRetrieval.java

License:Apache License

private MavenProject constructProject(final Artifact artifact, final MavenSession session)
        throws SourceRetrievalException {
    try {/* w  w w.  j ava 2 s . c  o  m*/
        // pom files are not set up in projects which are not in the workspace, we add them in manually since they are needed
        Artifact pomArtifact = repositorySystem.createProjectArtifact(artifact.getGroupId(),
                artifact.getArtifactId(), artifact.getVersion());
        pomArtifact = resolveArtifact(pomArtifact, session);

        ProjectBuildingRequest request = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
        request.setActiveProfileIds(null);
        request.setInactiveProfileIds(null);
        request.setUserProperties(null);

        ProjectBuildingResult result = projectBuilder.build(artifact, request);

        MavenProject mavenProject = result.getProject();
        mavenProject.setArtifact(resolveArtifact(mavenProject.getArtifact(), session));
        mavenProject.setFile(pomArtifact.getFile());
        return mavenProject;
    } catch (ProjectBuildingException e) {
        throw new SourceRetrievalException(e);
    }
}