Example usage for org.apache.maven.artifact.resolver.filter ScopeArtifactFilter ScopeArtifactFilter

List of usage examples for org.apache.maven.artifact.resolver.filter ScopeArtifactFilter ScopeArtifactFilter

Introduction

In this page you can find the example usage for org.apache.maven.artifact.resolver.filter ScopeArtifactFilter ScopeArtifactFilter.

Prototype

public ScopeArtifactFilter(String scope) 

Source Link

Usage

From source file:cn.wanghaomiao.maven.plugin.seimi.overlay.OverlayManager.java

License:Apache License

/**
 * Returns a list of WAR {@link org.apache.maven.artifact.Artifact} describing the overlays of the current project.
 *
 * @return the overlays as artifacts objects
 *//*  w  w  w.  j  av a2 s  .  c  o  m*/
private List<Artifact> getOverlaysAsArtifacts() {
    ScopeArtifactFilter filter = new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME);
    @SuppressWarnings("unchecked")
    final Set<Artifact> artifacts = project.getArtifacts();

    final List<Artifact> result = new ArrayList<Artifact>();
    for (Artifact artifact : artifacts) {
        if (!artifact.isOptional() && filter.include(artifact) && ("war".equals(artifact.getType()))) {
            result.add(artifact);
        }
    }
    return result;
}

From source file:cn.wanghaomiao.maven.plugin.seimi.packaging.ArtifactsPackagingTask.java

License:Apache License

/**
 * {@inheritDoc}//from   w ww . ja  va  2  s.  c o  m
 */
public void performPackaging(WarPackagingContext context) throws MojoExecutionException {
    try {
        final ScopeArtifactFilter filter = new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME);
        final List<String> duplicates = findDuplicates(context, artifacts);

        for (Artifact artifact : artifacts) {
            String targetFileName = getArtifactFinalName(context, artifact);

            context.getLog().debug("Processing: " + targetFileName);

            if (duplicates.contains(targetFileName)) {
                context.getLog().debug("Duplicate found: " + targetFileName);
                targetFileName = artifact.getGroupId() + "-" + targetFileName;
                context.getLog().debug("Renamed to: " + targetFileName);
            }
            context.getWebappStructure().registerTargetFileName(artifact, targetFileName);

            if (!artifact.isOptional() && filter.include(artifact)) {
                try {
                    String type = artifact.getType();
                    if ("tld".equals(type)) {
                        copyFile(id, context, artifact.getFile(), TLD_PATH + targetFileName);
                    } else if ("aar".equals(type)) {
                        copyFile(id, context, artifact.getFile(), SERVICES_PATH + targetFileName);
                    } else if ("mar".equals(type)) {
                        copyFile(id, context, artifact.getFile(), MODULES_PATH + targetFileName);
                    } else if ("xar".equals(type)) {
                        copyFile(id, context, artifact.getFile(), EXTENSIONS_PATH + targetFileName);
                    } else if ("jar".equals(type) || "ejb".equals(type) || "ejb-client".equals(type)
                            || "test-jar".equals(type) || "bundle".equals(type)) {
                        copyFile(id, context, artifact.getFile(), LIB_PATH + targetFileName);
                    } else if ("par".equals(type)) {
                        targetFileName = targetFileName.substring(0, targetFileName.lastIndexOf('.')) + ".jar";
                        copyFile(id, context, artifact.getFile(), LIB_PATH + targetFileName);
                    } else if ("war".equals(type)) {
                        // Nothing to do here, it is an overlay and it's already handled
                        context.getLog()
                                .debug("war artifacts are handled as overlays, ignoring [" + artifact + "]");
                    } else if ("zip".equals(type)) {
                        // Nothing to do here, it is an overlay and it's already handled
                        context.getLog()
                                .debug("zip artifacts are handled as overlays, ignoring [" + artifact + "]");
                    } else {
                        context.getLog().debug("Artifact of type [" + type + "] is not supported, ignoring ["
                                + artifact + "]");
                    }
                } catch (IOException e) {
                    throw new MojoExecutionException("Failed to copy file for artifact [" + artifact + "]", e);
                }
            }
        }
    } catch (InterpolationException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}

From source file:com.atolcd.alfresco.AmpMojo.java

public void execute() throws MojoExecutionException {
    // Module properties
    getLog().info("Processing module.properties file");
    moduleProperties.put("module.id", project.getGroupId() + "." + project.getArtifactId());

    String projectVersion = project.getVersion();
    String finalVersion = VersionUtil.getFinalVersion(projectVersion);
    if (finalVersion == null) {
        // No valid version found in finalVersion
        throw new MojoExecutionException(
                "Invalid version \"" + projectVersion + "\", not matching \"[0..9]+(\\.[0..9]+)*\"");
    }//from w w  w.  j  a  v a  2s.  c  o m
    if (!projectVersion.equals(finalVersion)) {
        getLog().info("Filtering version to have a valid alfresco version");
    }
    getLog().info("Using final version " + finalVersion);

    moduleProperties.put("module.version", finalVersion);

    File modulePropertiesFile = new File(targetDirectory, "module.properties");
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(modulePropertiesFile);
        moduleProperties.store(fos, null);
    } catch (IOException e) {
        throw new MojoExecutionException("Could not process module.properties", e);
    } finally {
        IOUtil.close(fos);
    }
    zipArchiver.addFile(modulePropertiesFile, "module.properties");

    // File-mapping properties file (automatically created for Share projects)
    if (shareModule) {
        if (filemappingProperties == null) {
            filemappingProperties = new Properties();
        }
        filemappingProperties.put("/web", "/");
    }
    if (filemappingProperties != null && !filemappingProperties.isEmpty()) {
        if (!filemappingProperties.containsKey("include.default")) {
            filemappingProperties.put("include.default", "true");
        }

        File filemappingPropertiesFile = new File(targetDirectory, "file-mapping.properties");
        try {
            fos = new FileOutputStream(filemappingPropertiesFile);
            filemappingProperties.store(fos, null);
        } catch (IOException e) {
            throw new MojoExecutionException("Could not process file-mapping.properties", e);
        } finally {
            IOUtil.close(fos);
        }
        zipArchiver.addFile(filemappingPropertiesFile, "file-mapping.properties");
    }

    // Alfresco configuration files
    // Mapped from configured resources to their respective target paths
    getLog().info("Adding configuration files");
    MavenResourcesExecution resourcesExecution;
    File targetConfigDirectory = new File(targetDirectory, "config");
    targetConfigDirectory.mkdir();
    try {
        if (StringUtils.isEmpty(encoding)) {
            getLog().warn("File encoding has not been set, using platform encoding "
                    + ReaderFactory.FILE_ENCODING + ", i.e. build is platform dependent!");
        }

        resourcesExecution = new MavenResourcesExecution(project.getResources(), targetConfigDirectory, project,
                encoding, null, Collections.<String>emptyList(), session);
        resourcesFiltering.filterResources(resourcesExecution);
    } catch (MavenFilteringException e) {
        throw new MojoExecutionException("Could not filter resources", e);
    }
    zipArchiver.addDirectory(targetConfigDirectory, "config/");

    // Web sources directory
    if (webDirectory.exists()) {
        getLog().info("Adding web sources");
        zipArchiver.addDirectory(webDirectory, "web/");
    }

    // Web build directory (minified sources)
    if (webBuildDirectory.exists()) {
        getLog().info("Adding minified web sources");
        zipArchiver.addDirectory(webBuildDirectory, "web/");
    }

    // Webscripts
    if (webscriptsDirectory.exists()) {
        getLog().info("Adding webscripts");
        zipArchiver.addDirectory(webscriptsDirectory, "config/alfresco/extension/templates/webscripts/");
    }

    // Alfresco webscripts overrides
    if (alfrescoWebscriptsDirectory.exists()) {
        getLog().info("Adding webscripts overrides");
        zipArchiver.addDirectory(alfrescoWebscriptsDirectory,
                "config/alfresco/templates/webscripts/org/alfresco/");
    }

    // Licenses
    if (licensesDirectory.exists()) {
        getLog().info("Adding licenses");
        zipArchiver.addDirectory(licensesDirectory, "licenses/");
    }

    // Root
    if (rootDirectory != null && rootDirectory.exists()) {
        getLog().info("Adding root directory files");
        zipArchiver.addDirectory(rootDirectory, "");
    }

    // JAR file
    MavenArchiver archiver = new MavenArchiver();
    archiver.setArchiver(jarArchiver);
    archiver.setOutputFile(jarFile);
    jarArchiver.addDirectory(outputDirectory, new String[] { "**/*.class" }, null);
    MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
    try {
        archiver.createArchive(session, project, archive);
    } catch (Exception e) {
        throw new MojoExecutionException("Could not build the jar file", e);
    }
    mavenProjectHelper.attachArtifact(project, "jar", "classes", jarFile);

    if (jarFile.exists()) {
        getLog().info("Adding JAR file");
        zipArchiver.addFile(jarFile, "lib/" + jarFile.getName());
    }

    // Dependencies (mapped to the AMP file "lib" directory)
    getLog().info("Adding JAR dependencies");
    Set<Artifact> artifacts = project.getArtifacts();
    for (Iterator<Artifact> iter = artifacts.iterator(); iter.hasNext();) {
        Artifact artifact = (Artifact) iter.next();
        ScopeArtifactFilter filter = new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME);
        if (!artifact.isOptional() && filter.include(artifact) && "jar".equals(artifact.getType())) {
            zipArchiver.addFile(artifact.getFile(), "lib/" + artifact.getFile().getName());
        }
    }

    File ampFile = new File(targetDirectory, project.getBuild().getFinalName() + ".amp");
    zipArchiver.setDestFile(ampFile);
    try {
        zipArchiver.createArchive();
    } catch (IOException e) {
        throw new MojoExecutionException("Could not build the amp file", e);
    }
    project.getArtifact().setFile(ampFile);
}

From source file:com.elasticgrid.maven.DeployMojo.java

License:Open Source License

@SuppressWarnings("unchecked")
public void execute() throws MojoExecutionException, MojoFailureException {
    File oar = new File(oarFileName);
    getLog().info("Deploying oar " + oar.getName() + "...");
    // TODO: copy the OAR to the dropBucket
    ArtifactFilter filter = new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME);
    try {//w  w w  .  j  av a  2 s . co m
        ArtifactResolutionResult result = resolver.resolveTransitively(dependencies, project.getArtifact(),
                localRepository, remoteRepositories, artifactMetadataSource, filter);
        Set<Artifact> artifacts = result.getArtifacts();

        for (Artifact artifact : artifacts) {
            getLog().info("Detected dependency: " + artifact + " available in " + artifact.getFile());
            // TODO: this is where the actual copy to the remote maven repository should occur!
        }
    } catch (ArtifactResolutionException e) {
        throw new MojoFailureException(e, "Can't resolve artifact", "Can't resolve artifact");
    } catch (ArtifactNotFoundException e) {
        throw new MojoFailureException(e, "Can't find artifact", "Can't find artifact");
    }

    List<Artifact> attachments = project.getAttachedArtifacts();
    getLog().info("Found " + attachments.size() + " attachments");
    for (Artifact artifact : attachments) {
        getLog().info("Detected attachment: " + artifact + " available in " + artifact.getFile());
        // TODO: copy the artifacts to the maven repo too!
    }
}

From source file:com.enonic.cms.maven.plugin.AbstractPluginMojo.java

License:Apache License

@SuppressWarnings({ "unchecked" })
protected final Set<Artifact> getIncludedArtifacts() {
    final Set<Artifact> result = new HashSet<Artifact>();
    final Set<Artifact> artifacts = this.project.getArtifacts();
    final ScopeArtifactFilter filter = new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME);

    for (final Artifact artifact : artifacts) {
        if (filter.include(artifact) && !artifact.isOptional()) {
            result.add(artifact);//  w  w  w  .ja v  a2s  .  co  m
        }
    }

    return result;
}

From source file:com.github.veithen.alta.AbstractGenerateMojo.java

License:Apache License

public final void execute() throws MojoExecutionException, MojoFailureException {
    Log log = getLog();/*from w w  w  .ja  v  a2s .  c o  m*/
    if (skip) {
        log.info("Skipping plugin execution");
        return;
    }
    Template<Context> nameTemplate;
    try {
        nameTemplate = templateCompiler.compile(name);
    } catch (InvalidTemplateException ex) {
        throw new MojoExecutionException("Invalid destination name template", ex);
    }
    Template<Context> valueTemplate;
    try {
        valueTemplate = templateCompiler.compile(value);
    } catch (InvalidTemplateException ex) {
        throw new MojoExecutionException("Invalid value template", ex);
    }
    List<Artifact> resolvedArtifacts = new ArrayList<Artifact>();

    if (dependencySet != null) {
        if (log.isDebugEnabled()) {
            log.debug("Resolving project dependencies in scope " + dependencySet.getScope());
        }
        AndArtifactFilter filter = new AndArtifactFilter();
        filter.add(new ScopeArtifactFilter(dependencySet.getScope()));
        filter.add(new IncludeExcludeArtifactFilter(dependencySet.getIncludes(), dependencySet.getExcludes(),
                null));
        for (Artifact artifact : project.getArtifacts()) {
            if (filter.include(artifact)) {
                resolvedArtifacts.add(artifact);
            }
        }
        if (dependencySet.isUseProjectArtifact()) {
            resolvedArtifacts.add(project.getArtifact());
        }
    }

    if (artifacts != null && artifacts.length != 0) {
        List<ArtifactRepository> pomRepositories = project.getRemoteArtifactRepositories();
        List<ArtifactRepository> effectiveRepositories;
        if (repositories != null && repositories.length > 0) {
            effectiveRepositories = new ArrayList<ArtifactRepository>(
                    pomRepositories.size() + repositories.length);
            effectiveRepositories.addAll(pomRepositories);
            for (Repository repository : repositories) {
                try {
                    effectiveRepositories.add(repositorySystem.buildArtifactRepository(repository));
                } catch (InvalidRepositoryException ex) {
                    throw new MojoExecutionException("Invalid repository", ex);
                }
            }
        } else {
            effectiveRepositories = pomRepositories;
        }
        for (ArtifactItem artifactItem : artifacts) {
            String version = artifactItem.getVersion();
            if (StringUtils.isEmpty(version)) {
                version = getMissingArtifactVersion(artifactItem);
            }
            Dependency dependency = new Dependency();
            dependency.setGroupId(artifactItem.getGroupId());
            dependency.setArtifactId(artifactItem.getArtifactId());
            dependency.setVersion(version);
            dependency.setType(artifactItem.getType());
            dependency.setClassifier(artifactItem.getClassifier());
            dependency.setScope(Artifact.SCOPE_COMPILE);
            Artifact artifact = repositorySystem.createDependencyArtifact(dependency);
            try {
                // Find an appropriate version in the specified version range
                ArtifactResolutionResult artifactResolutionResult = artifactCollector.collect(
                        Collections.singleton(artifact), project.getArtifact(), localRepository,
                        effectiveRepositories, artifactMetadataSource, null, Collections.EMPTY_LIST);
                artifact = ((ResolutionNode) artifactResolutionResult.getArtifactResolutionNodes().iterator()
                        .next()).getArtifact();

                // Download the artifact
                resolver.resolve(artifact, effectiveRepositories, localRepository);
            } catch (ArtifactResolutionException ex) {
                throw new MojoExecutionException("Unable to resolve artifact", ex);
            } catch (ArtifactNotFoundException ex) {
                throw new MojoExecutionException("Artifact not found", ex);
            }
            resolvedArtifacts.add(artifact);
        }
    }

    Map<String, String> result = new HashMap<String, String>();
    for (Artifact artifact : resolvedArtifacts) {
        if (log.isDebugEnabled()) {
            log.debug("Processing artifact " + artifact.getId());
        }
        Context context = new Context(artifact);
        try {
            String name = nameTemplate.evaluate(context);
            if (log.isDebugEnabled()) {
                log.debug("name = " + name);
            }
            if (name == null) {
                continue;
            }
            String value = valueTemplate.evaluate(context);
            if (log.isDebugEnabled()) {
                log.debug("value = " + value);
            }
            if (value == null) {
                continue;
            }
            String currentValue = result.get(name);
            if (currentValue == null) {
                currentValue = value;
            } else if (separator == null) {
                throw new MojoExecutionException("No separator configured");
            } else {
                currentValue = currentValue + separator + value;
            }
            result.put(name, currentValue);
        } catch (EvaluationException ex) {
            throw new MojoExecutionException(
                    "Failed to process artifact " + artifact.getId() + ": " + ex.getMessage(), ex);
        }
    }
    process(result);
}

From source file:com.github.wix_maven.PatchMojo.java

License:Apache License

protected Set<Artifact> resolveDependencyArtifacts(MavenProject theProject)
        throws ArtifactResolutionException, ArtifactNotFoundException, InvalidDependencyVersionException {
    Set<Artifact> artifacts = theProject.createArtifacts(this.factory, null,
            new ScopeArtifactFilter(Artifact.SCOPE_COMPILE));
    // doubt: scope if not null is ignoring the Provided scope elements - all examples show using some scope  

    getLog().info("Checking " + artifacts.size() + " dependents of " + theProject.getId());
    for (Artifact artifact : artifacts) {
        getLog().debug("Checking dependent " + artifact.getId());
        // resolve the new artifact
        this.resolver.resolve(artifact, this.remoteArtifactRepositories, this.localRepository);
    }/*w ww . j  a  v a2  s .  co m*/
    return artifacts;
}

From source file:com.google.code.play2.plugin.AbstractPlay2Mojo.java

License:Apache License

/**
 * This method resolves the dependency artifacts from the project.
 * /*from  www  .  j  a v a2 s.  c o  m*/
 * @param theProject The POM.
 * @return resolved set of dependency artifacts.
 * @throws ArtifactResolutionException
 * @throws ArtifactNotFoundException
 * @throws InvalidDependencyVersionException
 */
private Set<Artifact> resolveDependencyArtifacts(MavenProject theProject)
        throws ArtifactNotFoundException, ArtifactResolutionException, InvalidDependencyVersionException {
    AndArtifactFilter filter = new AndArtifactFilter();
    filter.add(new ScopeArtifactFilter(Artifact.SCOPE_TEST));
    filter.add(new NonOptionalArtifactFilter());
    // TODO follow the dependenciesManagement and override rules
    Set<Artifact> artifacts = theProject.createArtifacts(factory, Artifact.SCOPE_RUNTIME, filter);
    for (Artifact artifact : artifacts) {
        resolver.resolve(artifact, remoteRepos, localRepo);
    }
    return artifacts;
}

From source file:com.kamomileware.maven.plugin.opencms.packaging.ArtifactsPackagingTask.java

License:Apache License

public void performPackaging(ModulePackagingContext context) throws MojoExecutionException {
    try {/*from w  w  w .j a  va  2 s  .  c  o m*/
        final ScopeArtifactFilter filter = new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME);
        final List<String> duplicates = findDuplicates(context, artifacts);
        final String prefix = context.getModuleSourceTargetDirectory() == null ? ""
                : context.getModuleSourceTargetDirectory().endsWith("/")
                        ? context.getModuleSourceTargetDirectory()
                        : context.getModuleSourceTargetDirectory().concat("/");

        for (Iterator<Artifact> iter = artifacts.iterator(); iter.hasNext();) {
            Artifact artifact = (Artifact) iter.next();
            String targetFileName = getArtifactFinalName(context, artifact);

            context.getLog().debug("Processing: " + targetFileName);

            if (duplicates.contains(targetFileName)) {
                context.getLog().debug("Duplicate found: " + targetFileName);
                targetFileName = artifact.getGroupId() + "-" + targetFileName;
                context.getLog().debug("Renamed to: " + targetFileName);
            }
            context.getModuleStructure().registerTargetFileName(artifact, targetFileName);

            if (!artifact.isOptional() && filter.include(artifact)) {
                try {
                    String type = artifact.getType();
                    if ("tld".equals(type)) {
                        copyFile(id, context, artifact.getFile(), TLD_PATH + targetFileName, false);
                    } else if ("aar".equals(type)) {
                        copyFile(id, context, artifact.getFile(), SERVICES_PATH + targetFileName, false);
                    } else if ("jar".equals(type) || "ejb".equals(type) || "ejb-client".equals(type)
                            || "test-jar".equals(type)) {
                        copyFile(id, context, artifact.getFile(), prefix + LIB_PATH + targetFileName, false);
                    } else if ("par".equals(type)) {
                        targetFileName = targetFileName.substring(0, targetFileName.lastIndexOf('.')) + ".jar";
                        copyFile(id, context, artifact.getFile(), prefix + LIB_PATH + targetFileName, false);
                    } else if ("war".equals(type)) {
                        // Nothing to do here, it is an overlay and it's
                        // already handled
                        context.getLog()
                                .debug("war artifacts are handled as overlays, ignoring[" + artifact + "]");
                    } else if ("zip".equals(type)) {
                        // Nothing to do here, it is an overlay and it's
                        // already handled
                        context.getLog()
                                .debug("zip artifacts are handled as overlays, ignoring[" + artifact + "]");
                    } else {
                        context.getLog().debug(
                                "Artifact of type[" + type + "] is not supported, ignoring[" + artifact + "]");
                    }
                } catch (IOException e) {
                    throw new MojoExecutionException("Failed to copy file for artifact[" + artifact + "]", e);
                }
            }
        }
    } catch (InterpolationException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}

From source file:com.kw.opencms.module.mojo.packaging.ArtifactsPackagingTask.java

License:Apache License

public void performPackaging(ModulePackagingContext context) throws MojoExecutionException {
    try {/*ww w .  ja v a  2s .c om*/
        final ScopeArtifactFilter filter = new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME);
        final List duplicates = findDuplicates(context, artifacts);
        final String prefix = context.getModuleSourceTargetDirectory() == null ? ""
                : context.getModuleSourceTargetDirectory().endsWith("/")
                        ? context.getModuleSourceTargetDirectory()
                        : context.getModuleSourceTargetDirectory().concat("/");

        for (Iterator iter = artifacts.iterator(); iter.hasNext();) {
            Artifact artifact = (Artifact) iter.next();
            String targetFileName = getArtifactFinalName(context, artifact);

            context.getLog().debug("Processing: " + targetFileName);

            if (duplicates.contains(targetFileName)) {
                context.getLog().debug("Duplicate found: " + targetFileName);
                targetFileName = artifact.getGroupId() + "-" + targetFileName;
                context.getLog().debug("Renamed to: " + targetFileName);
            }
            context.getModuleStructure().registerTargetFileName(artifact, targetFileName);

            if (!artifact.isOptional() && filter.include(artifact)) {
                try {
                    String type = artifact.getType();
                    if ("tld".equals(type)) {
                        copyFile(id, context, artifact.getFile(), TLD_PATH + targetFileName, false);
                    } else if ("aar".equals(type)) {
                        copyFile(id, context, artifact.getFile(), SERVICES_PATH + targetFileName, false);
                    } else if ("jar".equals(type) || "ejb".equals(type) || "ejb-client".equals(type)
                            || "test-jar".equals(type)) {
                        copyFile(id, context, artifact.getFile(), prefix + LIB_PATH + targetFileName, false);
                    } else if ("par".equals(type)) {
                        targetFileName = targetFileName.substring(0, targetFileName.lastIndexOf('.')) + ".jar";
                        copyFile(id, context, artifact.getFile(), prefix + LIB_PATH + targetFileName, false);
                    } else if ("war".equals(type)) {
                        // Nothing to do here, it is an overlay and it's already handled
                        context.getLog()
                                .debug("war artifacts are handled as overlays, ignoring[" + artifact + "]");
                    } else if ("zip".equals(type)) {
                        // Nothing to do here, it is an overlay and it's already handled
                        context.getLog()
                                .debug("zip artifacts are handled as overlays, ignoring[" + artifact + "]");
                    } else {
                        context.getLog().debug(
                                "Artifact of type[" + type + "] is not supported, ignoring[" + artifact + "]");
                    }
                } catch (IOException e) {
                    throw new MojoExecutionException("Failed to copy file for artifact[" + artifact + "]", e);
                }
            }
        }
    } catch (InterpolationException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}