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:org.nuxeo.build.ant.artifact.AttachArtifactTask.java

License:Open Source License

@Override
public void execute() throws BuildException {
    MavenProject pom = AntBuildMojo.getInstance().getProject();
    log("Attaching " + file + " to " + pom, Project.MSG_INFO);
    if (type == null) {
        type = getExtension(file.getName());
        log("Unspecified type, using: " + type, Project.MSG_WARN);
    }//from   w  w  w .jav  a 2  s .c  o  m
    Artifact pomArtifact = pom.getArtifact();
    if (classifier != null || !type.equals(pomArtifact.getType())) {
        AntBuildMojo.getInstance().getProjectHelper().attachArtifact(pom, type, classifier, file);
    } else {
        pomArtifact.setFile(file);
        pomArtifact.setResolved(true);
    }
}

From source file:org.nuxeo.build.maven.AntBuildMojo.java

License:Open Source License

@Override
public void resolveDependencyTree(Artifact artifact, ArtifactFilter filter, ResolutionListener listener)
        throws ArtifactResolutionException, ProjectBuildingException {
    MavenProject mavenProject = projectBuilder.buildFromRepository(artifact, remoteRepositories,
            localRepository);/*  w ww .j  a v a 2  s  .  c o m*/
    ArtifactCollector collector = new DefaultArtifactCollector();
    collector.collect(mavenProject.getDependencyArtifacts(), mavenProject.getArtifact(),
            mavenProject.getManagedVersionMap(), localRepository, mavenProject.getRemoteArtifactRepositories(),
            metadataSource, filter, Collections.singletonList(listener));
}

From source file:org.nuxeo.build.maven.EmbeddedMavenClient.java

License:Open Source License

public void resolveDependencyTree(Artifact artifact, ArtifactFilter filter, ResolutionListener listener)
        throws ArtifactResolutionException, ProjectBuildingException {
    MavenProject project = mavenProjectBuilder.buildFromRepository(artifact, getRemoteRepositories(),
            localRepository);/* www  .  jav  a2  s .  c om*/

    @SuppressWarnings("rawtypes")
    Set dependencyArtifacts = project.getDependencyArtifacts();
    if (dependencyArtifacts == null) {
        try {
            dependencyArtifacts = project.createArtifacts(artifactFactory, null, null);
        } catch (InvalidDependencyVersionException e) {
            throw new ArtifactResolutionException("Cannot set dependencies", artifact, e);
        }
        project.setDependencyArtifacts(dependencyArtifacts);
    }

    ArtifactCollector collector = new DefaultArtifactCollector();
    collector.collect(dependencyArtifacts, project.getArtifact(), project.getManagedVersionMap(),
            localRepository, project.getRemoteArtifactRepositories(), artifactMetadataSource, filter,
            Collections.singletonList(listener));
}

From source file:org.nuxeo.build.maven.graph.Graph.java

License:Open Source License

/**
 * Add a root node given an artifact pom. This can be used by the embedder
 * maven mojo to initialize the graph with the current pom.
 *///from ww w  .  jav  a2  s. c  om
public Node addRootNode(MavenProject pom) {
    return getRootNode(pom, pom.getArtifact());
}

From source file:org.opennms.maven.plugins.tgz.AbstractAssemblyMojo.java

License:Apache License

private void processModules(Archiver archiver, List moduleSets, boolean includeBaseDirectory)
        throws MojoFailureException, MojoExecutionException {
    for (Iterator i = moduleSets.iterator(); i.hasNext();) {
        ModuleSet moduleSet = (ModuleSet) i.next();

        AndArtifactFilter filter = new AndArtifactFilter();

        if (!moduleSet.getIncludes().isEmpty()) {
            filter.add(new AssemblyIncludesArtifactFilter(moduleSet.getIncludes()));
        }//from  w  w w.  ja  va 2s . c  o  m
        if (!moduleSet.getExcludes().isEmpty()) {
            filter.add(new AssemblyExcludesArtifactFilter(moduleSet.getExcludes()));
        }

        Set set = getModulesFromReactor(getExecutedProject());

        List moduleFileSets = new ArrayList();

        for (Iterator j = set.iterator(); j.hasNext();) {
            MavenProject moduleProject = (MavenProject) j.next();

            if (filter.include(moduleProject.getArtifact())) {
                String name = moduleProject.getBuild().getFinalName();

                ModuleSources sources = moduleSet.getSources();

                if (sources != null) {
                    String output = sources.getOutputDirectory();
                    output = getOutputDirectory(output, moduleProject, includeBaseDirectory);

                    FileSet moduleFileSet = new FileSet();

                    moduleFileSet.setDirectory(moduleProject.getBasedir().getAbsolutePath());
                    moduleFileSet.setOutputDirectory(output);

                    List excludesList = new ArrayList();
                    excludesList.add(PathUtils.toRelative(moduleProject.getBasedir(),
                            moduleProject.getBuild().getDirectory()) + "/**");
                    excludesList.add(PathUtils.toRelative(moduleProject.getBasedir(),
                            moduleProject.getBuild().getOutputDirectory()) + "/**");
                    excludesList.add(PathUtils.toRelative(moduleProject.getBasedir(),
                            moduleProject.getBuild().getTestOutputDirectory()) + "/**");
                    excludesList.add(PathUtils.toRelative(moduleProject.getBasedir(),
                            moduleProject.getReporting().getOutputDirectory()) + "/**");
                    moduleFileSet.setExcludes(excludesList);

                    moduleFileSets.add(moduleFileSet);
                }

                ModuleBinaries binaries = moduleSet.getBinaries();

                if (binaries != null) {
                    Artifact artifact = moduleProject.getArtifact();

                    if (artifact.getFile() == null) {
                        throw new MojoExecutionException("Included module: " + moduleProject.getId()
                                + " does not have an artifact with a file. Please ensure the package phase is run before the assembly is generated.");
                    }

                    String output = binaries.getOutputDirectory();
                    output = getOutputDirectory(output, moduleProject, includeBaseDirectory);

                    archiver.setDefaultDirectoryMode(Integer.parseInt(binaries.getDirectoryMode(), 8));

                    archiver.setDefaultFileMode(Integer.parseInt(binaries.getFileMode(), 8));

                    getLog().debug("ModuleSet[" + output + "]" + " dir perms: "
                            + Integer.toString(archiver.getDefaultDirectoryMode(), 8) + " file perms: "
                            + Integer.toString(archiver.getDefaultFileMode(), 8));

                    if (binaries.isUnpack()) {
                        // TODO: something like zipfileset in plexus-archiver
                        //                        archiver.addJar(  )

                        // TODO refactor into the AbstractUnpackMojo
                        File tempLocation = new File(workDirectory, name);
                        boolean process = false;
                        if (!tempLocation.exists()) {
                            tempLocation.mkdirs();
                            process = true;
                        } else if (artifact.getFile().lastModified() > tempLocation.lastModified()) {
                            process = true;
                        }

                        if (process) {
                            try {
                                unpack(artifact.getFile(), tempLocation);

                                if (binaries.isIncludeDependencies()) {
                                    Set artifactSet = moduleProject.getArtifacts();

                                    for (Iterator artifacts = artifactSet.iterator(); artifacts.hasNext();) {
                                        Artifact dependencyArtifact = (Artifact) artifacts.next();

                                        unpack(dependencyArtifact.getFile(), tempLocation);
                                    }
                                }
                            } catch (NoSuchArchiverException e) {
                                throw new MojoExecutionException(
                                        "Unable to obtain unarchiver: " + e.getMessage(), e);
                            }

                            /*
                            * If the assembly is 'jar-with-dependencies', remove the security files in all dependencies
                            * that will prevent the uberjar to execute.  Please see MASSEMBLY-64 for details.
                            */
                            if (archiver instanceof JarArchiver) {
                                String[] securityFiles = { "*.RSA", "*.DSA", "*.SF", "*.rsa", "*.dsa", "*.sf" };
                                org.apache.maven.shared.model.fileset.FileSet securityFileSet = new org.apache.maven.shared.model.fileset.FileSet();
                                securityFileSet.setDirectory(tempLocation.getAbsolutePath() + "/META-INF/");

                                for (int sfsi = 0; sfsi < securityFiles.length; sfsi++) {
                                    securityFileSet.addInclude(securityFiles[sfsi]);
                                }

                                FileSetManager fsm = new FileSetManager(getLog());
                                try {
                                    fsm.delete(securityFileSet);
                                } catch (IOException e) {
                                    throw new MojoExecutionException(
                                            "Failed to delete security files: " + e.getMessage(), e);
                                }
                            }
                        }

                        addDirectory(archiver, tempLocation, output, null,
                                FileUtils.getDefaultExcludesAsList());
                    } else {
                        try {
                            String outputFileNameMapping = binaries.getOutputFileNameMapping();

                            archiver.addFile(artifact.getFile(),
                                    output + evaluateFileNameMapping(artifact, outputFileNameMapping));

                            if (binaries.isIncludeDependencies()) {
                                Set artifactSet = moduleProject.getArtifacts();

                                for (Iterator artifacts = artifactSet.iterator(); artifacts.hasNext();) {
                                    Artifact dependencyArtifact = (Artifact) artifacts.next();

                                    archiver.addFile(dependencyArtifact.getFile(),
                                            output + evaluateFileNameMapping(dependencyArtifact,
                                                    outputFileNameMapping));
                                }
                            }
                        } catch (ArchiverException e) {
                            throw new MojoExecutionException("Error adding file to archive: " + e.getMessage(),
                                    e);
                        }
                    }
                }

            } else {
                // would be better to have a way to find out when a specified include or exclude
                // is never triggered and warn() it.
                getLog().debug("module: " + moduleProject.getId() + " not included");
            }

            if (!moduleFileSets.isEmpty()) {
                // TODO: includes and excludes
                processFileSets(archiver, moduleFileSets, includeBaseDirectory);
            }
        }
    }
}

From source file:org.opennms.maven.plugins.tgz.AbstractUnpackingMojo.java

License:Apache License

/**
 * Retrieves all artifact dependencies.//from  ww  w  . j  a v a2 s. c  o  m
 *
 * @return A HashSet of artifacts
 */
protected Set getDependencies() {
    MavenProject project = getExecutedProject();

    Set dependenciesSet = new HashSet();

    if (project.getArtifact() != null && project.getArtifact().getFile() != null
            && shouldIncludeSelfAsDependency) {
        dependenciesSet.add(project.getArtifact());
    }

    Set projectArtifacts = project.getArtifacts();
    if (projectArtifacts != null) {
        dependenciesSet.addAll(projectArtifacts);
    }

    return dependenciesSet;
}

From source file:org.ops4j.pax.construct.lifecycle.EclipseOSGiMojo.java

License:Apache License

/**
 * @param bundleProject bundle project/*from  w  ww. ja va2 s  . c  om*/
 * @return recently built bundle
 */
private File getBundleFile(MavenProject bundleProject) {
    Artifact artifact = bundleProject.getArtifact();
    File bundleFile = artifact.getFile();

    if (null == bundleFile || !bundleFile.exists()) {
        // no file attached in this cycle, so check local build
        String name = bundleProject.getBuild().getFinalName() + ".jar";
        bundleFile = new File(bundleProject.getBuild().getDirectory(), name);
    }

    if (!bundleFile.exists()) {
        // last chance: see if it is already has been installed locally
        PomUtils.getFile(artifact, artifactResolver, localRepository);
        bundleFile = artifact.getFile();
    }

    return bundleFile;
}

From source file:org.ops4j.pax.construct.lifecycle.ProvisionMojo.java

License:Apache License

/**
 * Adds project artifact (if it's a bundle) to the deploy list as well as any non-optional bundle dependencies
 * /*from   w w w . ja va2 s . c o  m*/
 * @param project a Maven project
 * @param checkDependencies when true, check project dependencies for other bundles to provision
 */
private void addProjectBundles(MavenProject project, boolean checkDependencies) {
    if (PomUtils.isBundleProject(project, m_resolver, m_remoteRepos, m_localRepo, true)) {
        provisionBundle(project.getArtifact());
    }

    if (checkDependencies || isProvisioningPom(project)) {
        addProjectDependencies(project);
    }
}

From source file:org.ops4j.pax.construct.util.PomUtils.java

License:Apache License

/**
 * @param project Maven project/*from  ww w .  j  a  v a  2  s . c  o  m*/
 * @param resolver artifact resolver
 * @param remoteRepos sequence of remote repositories
 * @param localRepo local Maven repository
 * @param testMetadata check jar manifest for OSGi attributes if true
 * @return true if this is an OSGi bundle project, otherwise false
 */
public static boolean isBundleProject(MavenProject project, ArtifactResolver resolver, List remoteRepos,
        ArtifactRepository localRepo, boolean testMetadata) {
    String packaging = project.getPackaging();
    if (packaging != null && packaging.indexOf("bundle") >= 0) {
        return true;
    }

    return isBundleArtifact(project.getArtifact(), resolver, remoteRepos, localRepo, testMetadata);
}

From source file:org.ops4j.pax.exam.mavenplugin.GenerateConfigMojo.java

License:Apache License

protected void writeProvisioning(PrintStream out, List<Dependency> dependencies)
        throws ArtifactResolutionException, ArtifactNotFoundException {
    out.println("# provisioning");
    out.println();//from w  w w .  j  a va2  s  . c o m

    for (Dependency dependency : dependencies) {
        Artifact artifact = factory.createDependencyArtifact(dependency.getGroupId(),
                dependency.getArtifactId(), VersionRange.createFromVersion(dependency.getVersion()),
                dependency.getType(), dependency.getClassifier(), dependency.getScope());

        // try to find
        boolean found = false;
        for (MavenProject project : (List<MavenProject>) session.getSortedProjects()) {

            Artifact projectArtifact = project.getArtifact();
            if (projectArtifact.getArtifactId().equals(artifact.getArtifactId())
                    && (projectArtifact.getGroupId().equals(artifact.getGroupId())
                            && projectArtifact.getVersion().equals(artifact.getVersion()))) {
                artifact = projectArtifact;
                found = true;
                break;
            }

        }

        if (!found) {
            resolver.resolve(artifact, remoteRepositories, session.getLocalRepository());
        }

        out.println(

                createPaxRunnerScan(artifact, getSettingsForArtifact(settings.get(SETTINGS_DEPENDENCY_OPTIONS),
                        artifact.getGroupId(), artifact.getArtifactId())));

        getLog().debug("Dependency: " + dependency + " classifier: " + dependency.getClassifier() + " type: "
                + dependency.getType());
    }
    out.println();
}