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

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

Introduction

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

Prototype

public AndArtifactFilter() 

Source Link

Usage

From source file:br.com.anteros.restdoc.maven.plugin.AnterosRestDocMojo.java

License:Apache License

/**
 * Resolve dependency sources so they can be included directly in the
 * javadoc process. To customize this, override
 * {@link AbstractJavadocMojo#configureDependencySourceResolution(SourceResolverConfig)}.
 *//*from   w  w w .j a  va  2s  . co m*/
protected final List<String> getDependencySourcePaths() throws MavenReportException {
    try {
        if (sourceDependencyCacheDir.exists()) {
            FileUtils.forceDelete(sourceDependencyCacheDir);
            sourceDependencyCacheDir.mkdirs();
        }
    } catch (IOException e) {
        throw new MavenReportException(
                "Failed to delete cache directory: " + sourceDependencyCacheDir + "\nReason: " + e.getMessage(),
                e);
    }

    final SourceResolverConfig config = getDependencySourceResolverConfig();

    final AndArtifactFilter andFilter = new AndArtifactFilter();

    final List<String> dependencyIncludes = dependencySourceIncludes;
    final List<String> dependencyExcludes = dependencySourceExcludes;

    if (!includeTransitiveDependencySources || isNotEmpty(dependencyIncludes)
            || isNotEmpty(dependencyExcludes)) {
        if (!includeTransitiveDependencySources) {
            andFilter.add(createDependencyArtifactFilter());
        }

        if (isNotEmpty(dependencyIncludes)) {
            andFilter.add(new PatternIncludesArtifactFilter(dependencyIncludes, false));
        }

        if (isNotEmpty(dependencyExcludes)) {
            andFilter.add(new PatternExcludesArtifactFilter(dependencyExcludes, false));
        }

        config.withFilter(andFilter);
    }

    try {
        return ResourceResolver.resolveDependencySourcePaths(config);
    } catch (final ArtifactResolutionException e) {
        throw new MavenReportException(
                "Failed to resolve one or more javadoc source/resource artifacts:\n\n" + e.getMessage(), e);
    } catch (final ArtifactNotFoundException e) {
        throw new MavenReportException(
                "Failed to resolve one or more javadoc source/resource artifacts:\n\n" + e.getMessage(), e);
    }
}

From source file:com.github.ferstl.depgraph.AbstractGraphMojo.java

License:Apache License

private ArtifactFilter createGlobalArtifactFilter() {
    AndArtifactFilter filter = new AndArtifactFilter();

    if (this.scope != null) {
        filter.add(new ScopeArtifactFilter(this.scope));
    }//from   w ww .  j a  v a 2  s  . c om

    if (!this.includes.isEmpty()) {
        filter.add(new StrictPatternIncludesArtifactFilter(this.includes));
    }

    if (!this.excludes.isEmpty()) {
        filter.add(new StrictPatternExcludesArtifactFilter(this.excludes));
    }

    return filter;
}

From source file:com.github.ferstl.depgraph.AbstractGraphMojo.java

License:Apache License

private ArtifactFilter createTargetArtifactFilter() {
    AndArtifactFilter filter = new AndArtifactFilter();

    if (!this.targetIncludes.isEmpty()) {
        filter.add(new StrictPatternIncludesArtifactFilter(this.targetIncludes));
    }/*from www  . j ava2s  .  c o  m*/

    return filter;
}

From source file:com.github.jrh3k5.flume.mojo.plugin.AbstractFlumePluginMojo.java

License:Apache License

/**
 * Build a Flume plugin.// w w w  .  j a v  a2s.  c  o m
 * 
 * @param pluginLibrary
 *            A {@link File} representing the library that is to copied into the {@code lib/} directory of the plugin.
 * @param mavenProject
 *            A {@link MavenProject} representing the project from which dependency information should be read.
 * @throws MojoExecutionException
 *             If any errors occur during the bundling of the plugin archive.
 */
protected void buildFlumePluginArchive(File pluginLibrary, MavenProject mavenProject)
        throws MojoExecutionException {
    final String pluginName = getPluginName();
    // Create the directory into which the libraries will be copied
    final File pluginStagingDirectory = new File(pluginsStagingDirectory,
            String.format("%s-staging", pluginName));
    final File stagingDirectory = new File(pluginStagingDirectory, pluginName);
    try {
        FileUtils.forceMkdir(stagingDirectory);
    } catch (IOException e) {
        throw new MojoExecutionException("Failed to create directory: " + stagingDirectory.getAbsolutePath(),
                e);
    }

    final File libDirectory = new File(stagingDirectory, "lib");
    // Copy the primary library
    try {
        FileUtils.copyFile(pluginLibrary, new File(libDirectory, pluginLibrary.getName()));
    } catch (IOException e) {
        throw new MojoExecutionException(
                "Failed to copy primary artifact to staging lib directory: " + libDirectory.getAbsolutePath(),
                e);
    }

    // Copy the dependencies of the plugin into the libext directory
    final File libExtDirectory = new File(stagingDirectory, "libext");
    final AndArtifactFilter joinFilter = new AndArtifactFilter();
    joinFilter.add(providedArtifactFilter);
    joinFilter.add(new ExclusionArtifactFilter(exclusions));
    for (DependencyNode resolvedDependency : resolveDependencies(mavenProject, joinFilter)) {
        copyPluginDependency(resolvedDependency, libExtDirectory);
    }

    // Because of the way that Maven represents dependency trees, the above logic may copy the given plugin library into libext - remove it if so
    final File pluginLibraryLibExt = new File(libExtDirectory, pluginLibrary.getName());
    if (pluginLibraryLibExt.exists()) {
        try {
            FileUtils.forceDelete(pluginLibraryLibExt);
        } catch (IOException e) {
            throw new MojoExecutionException("Failed to delete file: " + pluginLibraryLibExt.getAbsolutePath(),
                    e);
        }
    }

    String classifier = null;
    // If the plugin name is the same as the artifact, then don't bother over-complicating the classifier
    if (project.getArtifactId().equals(pluginName)) {
        classifier = classifierSuffix;
    } else {
        classifier = String.format("%s-%s", pluginName, classifierSuffix);
    }
    final ArchiveUtils archiveUtils = ArchiveUtils.getInstance(new MojoLogger(getLog(), getClass()));
    // Create the TAR
    final File tarFile = new File(pluginStagingDirectory,
            String.format("%s-%s-%s.tar", project.getArtifactId(), project.getVersion(), classifier));
    try {
        archiveUtils.tarDirectory(pluginStagingDirectory, tarFile);
    } catch (IOException e) {
        throw new MojoExecutionException(String.format("Failed to TAR directory %s to file %s",
                stagingDirectory.getAbsolutePath(), tarFile.getAbsolutePath()), e);
    }

    // GZIP the TAR file
    final File gzipFile = new File(outputDirectory, String.format("%s.gz", tarFile.getName()));
    try {
        archiveUtils.gzipFile(tarFile, gzipFile);
    } catch (IOException e) {
        throw new MojoExecutionException(String.format("Failed to gzip TAR file %s to %s",
                tarFile.getAbsolutePath(), gzipFile.getAbsolutePath()), e);
    }

    // Attach the artifact, if configured to do so
    if (attach) {
        projectHelper.attachArtifact(project, "tar.gz", classifier, gzipFile);
    }
}

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

License:Apache License

public final void execute() throws MojoExecutionException, MojoFailureException {
    Log log = getLog();//  ww  w.ja v  a2  s .  com
    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(",");
    }//from ww  w. j a va  2s  . c  o 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  .  j av  a 2s.  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 w ww  .  j  av  a  2s .  co 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"
    }/*w w w  .  j  av  a  2s  . c om*/

    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.google.code.play2.plugin.AbstractPlay2Mojo.java

License:Apache License

/**
 * This method resolves the dependency artifacts from the project.
 * //from w  ww. j  a v a  2s . 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;
}