Example usage for org.apache.maven.artifact.resolver.filter AndArtifactFilter include

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

Introduction

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

Prototype

public boolean include(Artifact artifact) 

Source Link

Usage

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

License:Apache License

public final void execute() throws MojoExecutionException, MojoFailureException {
    Log log = getLog();//from  ww w . j  a  v  a2s .  co 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.google.code.play.AbstractPlayDistMojo.java

License:Apache License

protected ZipArchiver prepareArchiver(ConfigurationParser configParser)
        throws DependencyTreeBuilderException, IOException, MojoExecutionException, NoSuchArchiverException {
    ZipArchiver zipArchiver = getZipArchiver();

    File baseDir = project.getBasedir();

    Set<String> providedModuleNames = getProvidedModuleNames(configParser, playId, false);

    // APPLICATION
    getLog().debug("Dist application includes: " + distApplicationIncludes);
    getLog().debug("Dist application excludes: " + distApplicationExcludes);
    String[] applicationIncludes = null;
    if (distApplicationIncludes != null) {
        applicationIncludes = distApplicationIncludes.split(",");
    }/*w  w w  . j av  a2s. co m*/
    // TODO-don't add "test/**" if profile is not test profile
    String[] applicationExcludes = null;
    if (distApplicationExcludes != null) {
        applicationExcludes = distApplicationExcludes.split(",");
    }
    zipArchiver.addDirectory(baseDir, "application/", applicationIncludes, applicationExcludes);

    // preparation
    Set<?> projectArtifacts = project.getArtifacts();

    Set<Artifact> excludedArtifacts = new HashSet<Artifact>();
    Artifact playSeleniumJunit4Artifact = getDependencyArtifact(projectArtifacts,
            "com.google.code.maven-play-plugin", "play-selenium-junit4", "jar");
    if (playSeleniumJunit4Artifact != null) {
        excludedArtifacts.addAll(getDependencyArtifacts(projectArtifacts, playSeleniumJunit4Artifact));
    }

    AndArtifactFilter dependencyFilter = new AndArtifactFilter();
    if (distDependencyIncludes != null && distDependencyIncludes.length() > 0) {
        List<String> incl = Arrays.asList(distDependencyIncludes.split(","));
        PatternIncludesArtifactFilter includeFilter = new PatternIncludesArtifactFilter(incl,
                true/* actTransitively */ );

        dependencyFilter.add(includeFilter);
    }
    if (distDependencyExcludes != null && distDependencyExcludes.length() > 0) {
        List<String> excl = Arrays.asList(distDependencyExcludes.split(","));
        PatternExcludesArtifactFilter excludeFilter = new PatternExcludesArtifactFilter(excl,
                true/* actTransitively */ );

        dependencyFilter.add(excludeFilter);
    }

    Set<Artifact> filteredArtifacts = new HashSet<Artifact>(); // TODO-rename to filteredClassPathArtifacts
    for (Iterator<?> iter = projectArtifacts.iterator(); iter.hasNext();) {
        Artifact artifact = (Artifact) iter.next();
        if (artifact.getArtifactHandler().isAddedToClasspath() && !excludedArtifacts.contains(artifact)) {
            // TODO-add checkPotentialReactorProblem( artifact );
            if (dependencyFilter.include(artifact)) {
                filteredArtifacts.add(artifact);
            } else {
                getLog().debug(artifact.toString() + " excluded");
            }
        }
    }

    // framework
    getLog().debug("Dist framework includes: " + distFrameworkIncludes);
    getLog().debug("Dist framework excludes: " + distFrameworkExcludes);
    String[] frameworkIncludes = null;
    if (distFrameworkIncludes != null) {
        frameworkIncludes = distFrameworkIncludes.split(",");
    }
    String[] frameworkExcludes = null;
    if (distFrameworkExcludes != null) {
        frameworkExcludes = distFrameworkExcludes.split(",");
    }

    Artifact frameworkZipArtifact = findFrameworkArtifact(false);
    // TODO-validate not null
    File frameworkZipFile = frameworkZipArtifact.getFile();
    zipArchiver.addArchivedFileSet(frameworkZipFile, frameworkIncludes, frameworkExcludes);
    Artifact frameworkJarArtifact = getDependencyArtifact(filteredArtifacts/* ?? */,
            frameworkZipArtifact.getGroupId(), frameworkZipArtifact.getArtifactId(), "jar");
    // TODO-validate not null
    File frameworkJarFile = frameworkJarArtifact.getFile();
    String frameworkDestinationFileName = "framework/" + frameworkJarFile.getName();
    String playVersion = frameworkJarArtifact.getBaseVersion();
    if ("1.2".compareTo(playVersion) > 0) {
        // Play 1.1.x
        frameworkDestinationFileName = "framework/play.jar";
    }
    zipArchiver.addFile(frameworkJarFile, frameworkDestinationFileName);
    filteredArtifacts.remove(frameworkJarArtifact);
    Set<Artifact> dependencySubtree = getFrameworkDependencyArtifacts(filteredArtifacts, frameworkJarArtifact);
    for (Artifact classPathArtifact : dependencySubtree) {
        File jarFile = classPathArtifact.getFile();
        String destinationFileName = "framework/lib/" + jarFile.getName();
        zipArchiver.addFile(jarFile, destinationFileName);
        filteredArtifacts.remove(classPathArtifact);
    }

    // modules
    getLog().debug("Dist modules includes: " + distModulesIncludes);
    getLog().debug("Dist modules excludes: " + distModulesExcludes);
    String[] modulesIncludes = null;
    if (distModulesIncludes != null) {
        modulesIncludes = distModulesIncludes.split(",");
    }
    String[] modulesExcludes = null;
    if (distModulesExcludes != null) {
        modulesExcludes = distModulesExcludes.split(",");
    }

    // modules/*/lib and application/modules/*/lib
    Set<Artifact> notActiveProvidedModules = new HashSet<Artifact>();
    Map<String, Artifact> moduleArtifacts = findAllModuleArtifacts(false);
    for (Map.Entry<String, Artifact> moduleArtifactEntry : moduleArtifacts.entrySet()) {
        String moduleName = moduleArtifactEntry.getKey();
        Artifact moduleZipArtifact = moduleArtifactEntry.getValue();

        File moduleZipFile = moduleZipArtifact.getFile();
        if (Artifact.SCOPE_PROVIDED.equals(moduleZipArtifact.getScope())) {
            if (providedModuleNames.contains(moduleName)) {
                String moduleSubDir = String.format("modules/%s-%s/", moduleName,
                        moduleZipArtifact.getBaseVersion());
                if (isFrameworkEmbeddedModule(moduleName)) {
                    moduleSubDir = String.format("modules/%s/", moduleName);
                }
                zipArchiver.addArchivedFileSet(moduleZipFile, moduleSubDir, modulesIncludes, modulesExcludes);
                dependencySubtree = getModuleDependencyArtifacts(filteredArtifacts, moduleZipArtifact);
                for (Artifact classPathArtifact : dependencySubtree) {
                    File jarFile = classPathArtifact.getFile();
                    String destinationFileName = jarFile.getName();
                    // Scala module hack
                    if ("scala".equals(moduleName)) {
                        destinationFileName = scalaHack(classPathArtifact);
                    }
                    String destinationPath = String.format("%slib/%s", moduleSubDir, destinationFileName);
                    zipArchiver.addFile(jarFile, destinationPath);
                    filteredArtifacts.remove(classPathArtifact);
                }
            } else {
                notActiveProvidedModules.add(moduleZipArtifact);
            }
        } else {
            String moduleSubDir = String.format("application/modules/%s-%s/", moduleName,
                    moduleZipArtifact.getBaseVersion());
            zipArchiver.addArchivedFileSet(moduleZipFile, moduleSubDir, modulesIncludes, modulesExcludes);
            dependencySubtree = getModuleDependencyArtifacts(filteredArtifacts, moduleZipArtifact);
            for (Artifact classPathArtifact : dependencySubtree) {
                File jarFile = classPathArtifact.getFile();
                String destinationFileName = jarFile.getName();
                // Scala module hack
                if ("scala".equals(moduleName)) {
                    destinationFileName = scalaHack(classPathArtifact);
                }
                String destinationPath = String.format("application/modules/%s-%s/lib/%s", moduleName,
                        moduleZipArtifact.getBaseVersion(), destinationFileName);
                zipArchiver.addFile(jarFile, destinationPath);
                filteredArtifacts.remove(classPathArtifact);
            }
        }
    }

    for (Artifact moduleZipArtifact : notActiveProvidedModules) {
        dependencySubtree = getModuleDependencyArtifacts(filteredArtifacts, moduleZipArtifact);
        filteredArtifacts.removeAll(dependencySubtree);
    }

    // application/lib
    for (Iterator<?> iter = filteredArtifacts.iterator(); iter.hasNext();) {
        Artifact artifact = (Artifact) iter.next();
        File jarFile = artifact.getFile();
        String destinationFileName = "application/lib/" + jarFile.getName();
        zipArchiver.addFile(jarFile, destinationFileName);
    }

    checkArchiverForProblems(zipArchiver);

    return zipArchiver;
}

From source file:com.google.code.play.AbstractPlayWarMojo.java

License:Apache License

protected WarArchiver prepareArchiver(ConfigurationParser configParser, boolean addWarDirectory)
        throws DependencyTreeBuilderException, IOException, MojoExecutionException, NoSuchArchiverException {
    WarArchiver warArchiver = getWarArchiver();

    File playHome = getPlayHome();

    File baseDir = project.getBasedir();
    File buildDirectory = new File(project.getBuild().getDirectory());

    Set<String> providedModuleNames = getProvidedModuleNames(configParser, playWarId, true);

    // APPLICATION
    getLog().debug("War application includes: " + warApplicationIncludes);
    getLog().debug("War application excludes: " + warApplicationExcludes);
    String[] applicationIncludes = null;
    if (warApplicationIncludes != null) {
        applicationIncludes = warApplicationIncludes.split(",");
    }/*from w ww . ja  v a2s.c o  m*/
    // TODO-don't add "test/**" if profile is not test profile
    String[] applicationExcludes = null;
    if (warApplicationExcludes != null) {
        applicationExcludes = warApplicationExcludes.split(",");
    }
    warArchiver.addDirectory(baseDir, "WEB-INF/application/", applicationIncludes, applicationExcludes);

    getLog().debug("War conf classpath resources includes: " + warConfResourcesIncludes);
    getLog().debug("War conf classpath resources excludes: " + warConfResourcesExcludes);
    String[] confResourcesIncludes = null;
    if (warConfResourcesIncludes != null && warConfResourcesIncludes.length() > 0) {
        confResourcesIncludes = warConfResourcesIncludes.split(",");
    }
    String[] confResourcesExcludes = null;
    if (warConfResourcesExcludes != null && warConfResourcesExcludes.length() > 0) {
        confResourcesExcludes = warConfResourcesExcludes.split(",");
    }
    warArchiver.addClasses(new File(baseDir, "conf"), confResourcesIncludes, confResourcesExcludes);

    File webXmlFile = new File(warWebappDirectory, "WEB-INF/web.xml");
    if (!webXmlFile.isFile()) {
        webXmlFile = new File(playHome, "resources/war/web.xml");
    }
    if (warFilterWebXml) {
        File tmpDirectory = new File(buildDirectory, "play/tmp");
        webXmlFile = filterWebXml(webXmlFile, tmpDirectory, configParser.getApplicationName(), playWarId);
    }
    warArchiver.setWebxml(webXmlFile);

    // preparation
    Set<?> projectArtifacts = project.getArtifacts();

    Set<Artifact> excludedArtifacts = new HashSet<Artifact>();
    Artifact playSeleniumJunit4Artifact = getDependencyArtifact(projectArtifacts,
            "com.google.code.maven-play-plugin", "play-selenium-junit4", "jar");
    if (playSeleniumJunit4Artifact != null) {
        excludedArtifacts.addAll(getDependencyArtifacts(projectArtifacts, playSeleniumJunit4Artifact));
    }

    AndArtifactFilter dependencyFilter = new AndArtifactFilter();
    if (warDependencyIncludes != null && warDependencyIncludes.length() > 0) {
        List<String> incl = Arrays.asList(warDependencyIncludes.split(","));
        PatternIncludesArtifactFilter includeFilter = new PatternIncludesArtifactFilter(incl,
                true/* actTransitively */ );

        dependencyFilter.add(includeFilter);
    }
    if (warDependencyExcludes != null && warDependencyExcludes.length() > 0) {
        List<String> excl = Arrays.asList(warDependencyExcludes.split(","));
        PatternExcludesArtifactFilter excludeFilter = new PatternExcludesArtifactFilter(excl,
                true/* actTransitively */ );

        dependencyFilter.add(excludeFilter);
    }

    Set<Artifact> filteredArtifacts = new HashSet<Artifact>();
    for (Iterator<?> iter = projectArtifacts.iterator(); iter.hasNext();) {
        Artifact artifact = (Artifact) iter.next();
        if (artifact.getArtifactHandler().isAddedToClasspath() && !excludedArtifacts.contains(artifact)) {
            // TODO-add checkPotentialReactorProblem( artifact );
            if (dependencyFilter.include(artifact)) {
                filteredArtifacts.add(artifact);
            } else {
                getLog().debug(artifact.toString() + " excluded");
            }
        }
    }

    // framework
    getLog().debug("War framework includes: " + warFrameworkIncludes);
    getLog().debug("War framework excludes: " + warFrameworkExcludes);
    String[] frameworkIncludes = null;
    if (warFrameworkIncludes != null) {
        frameworkIncludes = warFrameworkIncludes.split(",");
    }
    String[] frameworkExcludes = null;
    if (warFrameworkExcludes != null) {
        frameworkExcludes = warFrameworkExcludes.split(",");
    }

    Artifact frameworkZipArtifact = findFrameworkArtifact(true);
    // TODO-validate not null
    File frameworkZipFile = frameworkZipArtifact.getFile();
    warArchiver.addArchivedFileSet(frameworkZipFile, "WEB-INF/", frameworkIncludes, frameworkExcludes);
    Artifact frameworkJarArtifact = getDependencyArtifact(filteredArtifacts, frameworkZipArtifact.getGroupId(),
            frameworkZipArtifact.getArtifactId(), "jar");
    // TODO-validate not null
    Set<Artifact> dependencySubtree = getFrameworkDependencyArtifacts(filteredArtifacts, frameworkJarArtifact);
    for (Artifact classPathArtifact : dependencySubtree) {
        File jarFile = classPathArtifact.getFile();
        warArchiver.addLib(jarFile);
        filteredArtifacts.remove(classPathArtifact);
    }

    // modules
    getLog().debug("War modules includes: " + warModulesIncludes);
    getLog().debug("War modules excludes: " + warModulesExcludes);
    String[] modulesIncludes = null;
    if (warModulesIncludes != null) {
        modulesIncludes = warModulesIncludes.split(",");
    }
    String[] modulesExcludes = null;
    if (warModulesExcludes != null) {
        modulesExcludes = warModulesExcludes.split(",");
    }

    Set<Artifact> notActiveProvidedModules = new HashSet<Artifact>();
    Map<String, Artifact> moduleArtifacts = findAllModuleArtifacts(false);
    for (Map.Entry<String, Artifact> moduleArtifactEntry : moduleArtifacts.entrySet()) {
        String moduleName = moduleArtifactEntry.getKey();
        Artifact moduleZipArtifact = moduleArtifactEntry.getValue();

        File moduleZipFile = moduleZipArtifact.getFile();
        String moduleSubDir = String.format("WEB-INF/application/modules/%s-%s/", moduleName,
                moduleZipArtifact.getBaseVersion());
        if (Artifact.SCOPE_PROVIDED.equals(moduleZipArtifact.getScope())) {
            if (providedModuleNames.contains(moduleName)) {
                moduleSubDir = String.format("WEB-INF/modules/%s-%s/", moduleName,
                        moduleZipArtifact.getBaseVersion());
                if (isFrameworkEmbeddedModule(moduleName)) {
                    moduleSubDir = String.format("WEB-INF/modules/%s/", moduleName);
                }
                warArchiver.addArchivedFileSet(moduleZipFile, moduleSubDir, modulesIncludes, modulesExcludes);
                dependencySubtree = getModuleDependencyArtifacts(filteredArtifacts, moduleZipArtifact);
                for (Artifact classPathArtifact : dependencySubtree) {
                    File jarFile = classPathArtifact.getFile();
                    warArchiver.addLib(jarFile);
                    filteredArtifacts.remove(classPathArtifact);
                }
                // Scala hack - NOT NEEDED, war works without it (maybe bacause precompiled == true)
                //if ( "scala".equals( moduleName ) )
                //{
                //    ...
                //}
            } else {
                notActiveProvidedModules.add(moduleZipArtifact);
            }
        } else {
            warArchiver.addArchivedFileSet(moduleZipFile, moduleSubDir, modulesIncludes, modulesExcludes);
            dependencySubtree = getModuleDependencyArtifacts(filteredArtifacts, moduleZipArtifact);
            for (Artifact classPathArtifact : dependencySubtree) {
                File jarFile = classPathArtifact.getFile();
                warArchiver.addLib(jarFile);
                filteredArtifacts.remove(classPathArtifact);
            }
        }
    }

    for (Artifact moduleZipArtifact : notActiveProvidedModules) {
        dependencySubtree = getModuleDependencyArtifacts(filteredArtifacts, moduleZipArtifact);
        filteredArtifacts.removeAll(dependencySubtree);
    }

    // lib
    for (Iterator<?> iter = filteredArtifacts.iterator(); iter.hasNext();) {
        Artifact artifact = (Artifact) iter.next();
        // TODO-exclude test-scoped dependencies?
        File jarFile = artifact.getFile();
        warArchiver.addLib(jarFile);
    }

    if (addWarDirectory) {
        if (warWebappDirectory.isDirectory()) {
            String[] webappIncludes = null;
            if (getWebappIncludes() != null) {
                webappIncludes = getWebappIncludes().split(",");
            }
            String[] webappExcludes = null;
            if (getWebappExcludes() != null) {
                webappExcludes = getWebappExcludes().split(",");
            }
            warArchiver.addDirectory(warWebappDirectory, webappIncludes, webappExcludes);
        }
    }

    checkArchiverForProblems(warArchiver);

    return warArchiver;
}

From source file:com.google.code.play.PlayZipMojo.java

License:Apache License

private void processDependencies(ZipArchiver zipArchiver) throws DependencyTreeBuilderException, IOException {
    // preparation
    Set<?> projectArtifacts = project.getArtifacts();

    Set<Artifact> excludedArtifacts = new HashSet<Artifact>();
    /*Artifact playSeleniumJunit4Artifact =
    getDependencyArtifact( projectArtifacts, "com.google.code.maven-play-plugin", "play-selenium-junit4",
                           "jar" );//from www  . ja  va 2s .  c o m
    if ( playSeleniumJunit4Artifact != null )
    {
    excludedArtifacts.addAll( getDependencyArtifacts( projectArtifacts, playSeleniumJunit4Artifact ) );
    }*/

    AndArtifactFilter dependencyFilter = new AndArtifactFilter();
    if (zipDependencyIncludes != null && zipDependencyIncludes.length() > 0) {
        List<String> incl = Arrays.asList(zipDependencyIncludes.split(","));
        PatternIncludesArtifactFilter includeFilter = new PatternIncludesArtifactFilter(incl,
                true/* actTransitively */ );

        dependencyFilter.add(includeFilter);
    }
    if (zipDependencyExcludes != null && zipDependencyExcludes.length() > 0) {
        List<String> excl = Arrays.asList(zipDependencyExcludes.split(","));
        PatternExcludesArtifactFilter excludeFilter = new PatternExcludesArtifactFilter(excl,
                true/* actTransitively */ );

        dependencyFilter.add(excludeFilter);
    }

    Set<Artifact> filteredArtifacts = new HashSet<Artifact>(); // TODO-rename to filteredClassPathArtifacts
    for (Iterator<?> iter = projectArtifacts.iterator(); iter.hasNext();) {
        Artifact artifact = (Artifact) iter.next();
        if (artifact.getArtifactHandler().isAddedToClasspath() && !excludedArtifacts.contains(artifact)) {
            // TODO-add checkPotentialReactorProblem( artifact );
            if (dependencyFilter.include(artifact)) {
                filteredArtifacts.add(artifact);
            } else {
                getLog().debug(artifact.toString() + " excluded");
            }
        }
    }

    // modules
    getLog().debug("Zip modules includes: " + zipModulesIncludes);
    getLog().debug("Zip modules excludes: " + zipModulesExcludes);
    String[] modulesIncludes = null;
    if (zipModulesIncludes != null) {
        modulesIncludes = zipModulesIncludes.split(",");
    }
    String[] modulesExcludes = null;
    if (zipModulesExcludes != null) {
        modulesExcludes = zipModulesExcludes.split(",");
    }

    Map<String, Artifact> moduleArtifacts = findAllModuleArtifacts(false);
    for (Map.Entry<String, Artifact> moduleArtifactEntry : moduleArtifacts.entrySet()) {
        String moduleName = moduleArtifactEntry.getKey();
        Artifact moduleZipArtifact = moduleArtifactEntry.getValue();

        File moduleZipFile = moduleZipArtifact.getFile();
        String moduleSubDir = String.format("modules/%s-%s/", moduleName, moduleZipArtifact.getBaseVersion());
        zipArchiver.addArchivedFileSet(moduleZipFile, moduleSubDir, modulesIncludes, modulesExcludes);
        Set<Artifact> dependencySubtree = getModuleDependencyArtifacts(filteredArtifacts, moduleZipArtifact);
        for (Artifact classPathArtifact : dependencySubtree) {
            File jarFile = classPathArtifact.getFile();
            String destinationFileName = jarFile.getName();
            // Scala module hack
            if ("scala".equals(moduleName)) {
                destinationFileName = scalaHack(classPathArtifact);
            }
            String destinationPath = String.format("modules/%s-%s/lib/%s", moduleName,
                    moduleZipArtifact.getBaseVersion(), destinationFileName);
            zipArchiver.addFile(jarFile, destinationPath);
            filteredArtifacts.remove(classPathArtifact);
        }

    }

    // lib
    for (Iterator<?> iter = filteredArtifacts.iterator(); iter.hasNext();) {
        Artifact artifact = (Artifact) iter.next();
        File jarFile = artifact.getFile();
        String destinationFileName = "lib/" + jarFile.getName();
        zipArchiver.addFile(jarFile, destinationFileName);
    }
}

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

License:Apache License

protected ZipArchiver prepareArchiver() throws IOException, MojoExecutionException, NoSuchArchiverException {
    ZipArchiver zipArchiver = getZipArchiver();

    File baseDir = project.getBasedir();
    File buildDirectory = new File(project.getBuild().getDirectory());

    File projectArtifactFile = new File(buildDirectory, project.getBuild().getFinalName() + ".jar"); // project.getArtifact().getFile();
    if (!projectArtifactFile.isFile()) {
        throw new MojoExecutionException(
                String.format("%s not present", projectArtifactFile.getAbsolutePath()));
        // TODO - add info about running "mvn package first"
    }/*from w w  w. j  a v a2 s .com*/

    if (configFile != null && !configFile.isFile()) {
        throw new MojoExecutionException(String.format("%s not present", configFile.getAbsolutePath()));
    }

    String packageName = project.getArtifactId() + "-" + project.getVersion();

    String destinationFileName = packageName + "/lib/" + projectArtifactFile.getName();
    zipArchiver.addFile(projectArtifactFile, destinationFileName);

    if (distClassifierIncludes != null && distClassifierIncludes.length() > 0) {
        List<String> incl = Arrays.asList(distClassifierIncludes.split(","));
        for (String classifier : incl) {
            String projectAttachedArtifactFileName = String.format("%s-%s.jar",
                    project.getBuild().getFinalName(), classifier.trim());
            File projectAttachedArtifactFile = new File(buildDirectory, projectAttachedArtifactFileName);
            if (!projectAttachedArtifactFile.isFile()) {
                throw new MojoExecutionException(
                        String.format("%s not present", projectAttachedArtifactFile.getAbsolutePath()));
            }
            destinationFileName = packageName + "/lib/" + projectAttachedArtifactFile.getName();
            zipArchiver.addFile(projectAttachedArtifactFile, destinationFileName);
        }
    }

    // preparation
    Set<?> projectArtifacts = project.getArtifacts();

    Set<Artifact> excludedArtifacts = new HashSet<Artifact>();

    AndArtifactFilter dependencyFilter = new AndArtifactFilter();
    if (distDependencyIncludes != null && distDependencyIncludes.length() > 0) {
        List<String> incl = Arrays.asList(distDependencyIncludes.split(","));
        PatternIncludesArtifactFilter includeFilter = new PatternIncludesArtifactFilter(incl,
                true/* actTransitively */ );

        dependencyFilter.add(includeFilter);
    }
    if (distDependencyExcludes != null && distDependencyExcludes.length() > 0) {
        List<String> excl = Arrays.asList(distDependencyExcludes.split(","));
        PatternExcludesArtifactFilter excludeFilter = new PatternExcludesArtifactFilter(excl,
                true/* actTransitively */ );

        dependencyFilter.add(excludeFilter);
    }

    Set<Artifact> filteredArtifacts = new HashSet<Artifact>(); // TODO-rename to filteredClassPathArtifacts
    for (Iterator<?> iter = projectArtifacts.iterator(); iter.hasNext();) {
        Artifact artifact = (Artifact) iter.next();
        if (artifact.getArtifactHandler().isAddedToClasspath() && !excludedArtifacts.contains(artifact)) {
            // TODO-add checkPotentialReactorProblem( artifact );
            if (dependencyFilter.include(artifact)) {
                filteredArtifacts.add(artifact);
            } else {
                getLog().debug(artifact.toString() + " excluded");
            }
        }
    }

    // lib
    for (Iterator<?> iter = filteredArtifacts.iterator(); iter.hasNext();) {
        Artifact artifact = (Artifact) iter.next();
        File jarFile = artifact.getFile();
        StringBuilder dfnsb = new StringBuilder();
        dfnsb.append(artifact.getGroupId()).append('.').append(artifact.getArtifactId()).append('-')
                .append(artifact.getVersion());
        if (artifact.getClassifier() != null) {
            dfnsb.append('-').append(artifact.getClassifier());
        }
        dfnsb.append(".jar"); // TODO-get the real extension?
        String destFileName = dfnsb.toString();
        // destinationFileName = ;
        zipArchiver.addFile(jarFile, packageName + "/lib/" + destFileName/* jarFile.getName() */ );
    }

    File linuxStartFile = createLinuxStartFile(buildDirectory);
    zipArchiver.addFile(linuxStartFile, packageName + "/start");

    File windowsStartFile = createWindowsStartFile(buildDirectory);
    zipArchiver.addFile(windowsStartFile, packageName + "/start.bat");

    File readmeFile = new File(baseDir, "README");
    if (readmeFile.isFile()) {
        zipArchiver.addFile(readmeFile, packageName + "/" + readmeFile.getName());
    }

    if (configFile != null) {
        zipArchiver.addFile(configFile, packageName + "/" + configFile.getName());
    }

    checkArchiverForProblems(zipArchiver);

    return zipArchiver;
}

From source file:com.googlecode.mycontainer.maven.plugin.ExecMojo.java

License:Apache License

private List filterArtifacts(List artifacts, Collection dependencies) {
    AndArtifactFilter filter = new AndArtifactFilter();

    filter.add(new IncludesArtifactFilter(new ArrayList(dependencies))); // gosh

    List filteredArtifacts = new ArrayList();
    for (Iterator it = artifacts.iterator(); it.hasNext();) {
        Artifact artifact = (Artifact) it.next();
        if (filter.include(artifact)) {
            getLog().debug("filtering in " + artifact);
            filteredArtifacts.add(artifact);
        }//from w  w  w  .  j  a  va  2s .  c om
    }
    return filteredArtifacts;
}

From source file:com.tonicsystems.jarjar.JarJarMojo.java

License:Apache License

@SuppressWarnings("unchecked")
public void execute() throws MojoExecutionException {
    try {//from  www .j  av a2s  . co  m
        // VALIDATE INPUT / OUTPUT

        if (null == input && null != project.getArtifact()) {
            input = project.getArtifact().getFile().getAbsolutePath();
        }
        if ("{classes}".equals(input)) {
            input = project.getBuild().getOutputDirectory();
        }
        if ("{test-classes}".equals(input)) {
            input = project.getBuild().getTestOutputDirectory();
        }
        if (null == input || !new File(input).exists()) {
            getLog().info("Nothing to process");
            return;
        }

        if (null == output) {
            output = input;
        }

        final File inputFile = new File(input);
        final File outputFile = new File(output);

        final boolean inPlaceJarJar = inputFile.equals(outputFile);

        final File backupFile = new File(outputFile.getParentFile(), "original-" + outputFile.getName());
        if (backupFile.isDirectory() && backupFile.list().length == 0) {
            backupFile.delete();
        }
        if (!overwrite && (inPlaceJarJar && backupFile.exists() || !inPlaceJarJar && outputFile.exists())) {
            getLog().info("Already processed");
            return;
        }

        // SETUP JARJAR

        final MainProcessor processor = new MainProcessor(rules, getLog().isDebugEnabled(), skipManifest);
        final AndArtifactFilter filter = new AndArtifactFilter();
        if (null != includes) {
            filter.add(new StrictPatternIncludesArtifactFilter(includes));
        }
        if (null != excludes) {
            filter.add(new StrictPatternExcludesArtifactFilter(excludes));
        }

        // BUILD UBER-ZIP OF ARTIFACT + DEPENDENCIES

        getLog().info("Processing: " + inputFile);

        final File uberZip = new File(workingDirectory, "uber-" + inputFile.getName());
        final Archiver archiver = archiverManager.getArchiver("zip");

        archiver.setDestFile(uberZip);

        if (inputFile.isDirectory()) {
            archiver.addDirectory(inputFile);
        } else {
            archiver.addArchivedFileSet(inputFile);
        }

        for (final Artifact a : (Set<Artifact>) project.getArtifacts()) {
            if (filter.include(a)) {
                try {
                    archiver.addArchivedFileSet(a.getFile(), null, new String[] { "META-INF/MANIFEST.MF" });
                } catch (final Throwable e) {
                    getLog().info("Ignoring: " + a);
                    getLog().debug(e);
                }
            }
        }

        if (!archiver.getResources().hasNext()) {
            getLog().info("Nothing to JarJar");
            return;
        }

        archiver.createArchive();

        // JARJAR UBER-ZIP

        getLog().info("JarJar'ing to: " + outputFile);

        final File hullZip = new File(workingDirectory, "hull-" + inputFile.getName());

        StandaloneJarProcessor.run(uberZip, hullZip, processor, true);
        processor.strip(hullZip);

        final boolean toDirectory = outputFile.isDirectory() || !outputFile.exists() && inputFile.isDirectory();

        if (inPlaceJarJar) {
            try {
                getLog().info("Original: " + backupFile);
                FileUtils.rename(outputFile, backupFile);
            } catch (final Throwable e) {
                getLog().warn(e.toString());
            }
        }

        if (toDirectory) {
            outputFile.mkdirs();
            final UnArchiver unarchiver = archiverManager.getUnArchiver("zip");
            unarchiver.setDestDirectory(outputFile);
            unarchiver.setSourceFile(hullZip);
            unarchiver.extract();
        } else {
            FileUtils.copyFile(hullZip, outputFile);
        }
    } catch (final Throwable e) {
        throw new MojoExecutionException("Unable to JarJar: " + input + " cause: " + e.getMessage(), e);
    }
}

From source file:kr.motd.maven.exec.ExecMojo.java

License:Apache License

private List<Artifact> filterArtifacts(List<Artifact> artifacts, Collection<String> dependencies) {
    AndArtifactFilter filter = new AndArtifactFilter();

    filter.add(new IncludesArtifactFilter(new ArrayList<String>(dependencies))); // gosh

    List<Artifact> filteredArtifacts = new ArrayList<Artifact>();
    for (Artifact artifact : artifacts) {
        if (filter.include(artifact)) {
            getLog().debug("filtering in " + artifact);
            filteredArtifacts.add(artifact);
        }//from   w  w  w. j  a va 2  s  .c  o  m
    }
    return filteredArtifacts;
}

From source file:net.hasor.maven.ExecMojo.java

License:Apache License

private List<Artifact> filterArtifacts(List<Artifact> artifacts, Collection<String> dependencies) {
    AndArtifactFilter filter = new AndArtifactFilter();
    filter.add(new IncludesArtifactFilter(new ArrayList<String>(dependencies))); // gosh
    List<Artifact> filteredArtifacts = new ArrayList<Artifact>();
    for (Artifact artifact : artifacts) {
        if (filter.include(artifact)) {
            getLog().debug("filtering in " + artifact);
            filteredArtifacts.add(artifact);
        }/* ww  w  .  j a  va2s.  c  o m*/
    }
    return filteredArtifacts;
}

From source file:npanday.plugin.resolver.CopyDependenciesMojo.java

License:Apache License

public void execute() throws MojoExecutionException, MojoFailureException {
    String skipReason = "";
    if (!skip) {/*ww w  . ja v  a 2 s  .  com*/
        ArtifactType knownType = ArtifactType.getArtifactTypeForPackagingName(project.getPackaging());

        if (knownType.equals(ArtifactType.NULL)) {
            skip = true;
            skipReason = ", because the current project (type:" + project.getPackaging()
                    + ") is not built with NPanday";
        }
    }

    if (skip) {
        getLog().info("NPANDAY-158-001: Mojo for copying dependencies was intentionally skipped" + skipReason);
        return;
    }

    SettingsUtil.applyCustomSettings(getLog(), repositoryRegistry, settingsPath);

    AndArtifactFilter includeFilter = new AndArtifactFilter();

    OrArtifactFilter typeIncludes = new OrArtifactFilter();
    typeIncludes.add(new DotnetExecutableArtifactFilter());
    typeIncludes.add(new DotnetLibraryArtifactFilter());

    if (includePdbs) {
        typeIncludes.add(new DotnetSymbolsArtifactFilter());
    }

    includeFilter.add(typeIncludes);

    if (!Strings.isNullOrEmpty(includeScope)) {
        includeFilter.add(new ScopeArtifactFilter(includeScope));
    }

    Set<Artifact> artifacts;
    try {
        artifacts = dependencyResolution.require(project, LocalRepositoryUtil.create(localRepository),
                includeFilter);
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException(
                "NPANDAY-158-003: dependency resolution for scope " + includeScope + " failed!", e);
    }

    /**
     * Should be resolved, but then not copied
     */
    if (!Strings.isNullOrEmpty(excludeScope)) {
        includeFilter.add(new InversionArtifactFilter(new ScopeArtifactFilter(excludeScope)));
    }

    if (skipReactorArtifacts) {
        getLog().info("NPANDAY-158-008: " + reactorProjects);

        includeFilter.add(new InversionArtifactFilter(new ArtifactFilter() {
            public boolean include(Artifact artifact) {
                for (MavenProject project : reactorProjects) {
                    // we don't care about the type and the classifier here
                    if (project.getGroupId().equals(artifact.getGroupId())
                            && project.getArtifactId().equals(artifact.getArtifactId())
                            && project.getVersion().equals(artifact.getVersion())) {
                        return true;
                    }
                }
                return false;
            }
        }));
    }

    for (Artifact dependency : artifacts) {
        if (!includeFilter.include(dependency)) {
            getLog().debug("NPANDAY-158-006: dependency " + dependency + " was excluded");

            continue;
        }

        try {
            File targetFile = new File(outputDirectory, PathUtil.getPlainArtifactFileName(dependency));
            if (!targetFile.exists() || targetFile.lastModified() != dependency.getFile().lastModified()
                    || targetFile.length() != dependency.getFile().length()) {
                getLog().info("NPANDAY-158-004: copy " + dependency.getFile() + " to " + targetFile);
                FileUtils.copyFile(dependency.getFile(), targetFile);
            } else {
                getLog().debug("NPANDAY-158-007: dependency " + dependency + " is yet up to date");
            }
        } catch (IOException ioe) {
            throw new MojoExecutionException("NPANDAY-158-005: Error copying dependency " + dependency, ioe);
        }
    }
}