Search in sources :

Example 11 with ScopeArtifactFilter

use of org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter in project aries by apache.

the class EbaMojo method execute.

public void execute() throws MojoExecutionException {
    getLog().debug(" ======= EbaMojo settings =======");
    getLog().debug("ebaSourceDirectory[" + ebaSourceDirectory + "]");
    getLog().debug("manifestFile[" + manifestFile + "]");
    getLog().debug("applicationManifestFile[" + applicationManifestFile + "]");
    getLog().debug("workDirectory[" + workDirectory + "]");
    getLog().debug("outputDirectory[" + outputDirectory + "]");
    getLog().debug("finalName[" + finalName + "]");
    getLog().debug("generateManifest[" + generateManifest + "]");
    if (archiveContent == null) {
        archiveContent = new String("applicationContent");
    }
    getLog().debug("archiveContent[" + archiveContent + "]");
    getLog().info("archiveContent[" + archiveContent + "]");
    zipArchiver.setIncludeEmptyDirs(includeEmptyDirs);
    zipArchiver.setCompress(true);
    zipArchiver.setForced(forceCreation);
    // Check if jar file is there and if requested, copy it
    try {
        if (includeJar.booleanValue()) {
            File generatedJarFile = new File(outputDirectory, finalName + ".jar");
            if (generatedJarFile.exists()) {
                getLog().info("Including generated jar file[" + generatedJarFile.getName() + "]");
                zipArchiver.addFile(generatedJarFile, finalName + ".jar");
            }
        }
    } catch (ArchiverException e) {
        throw new MojoExecutionException("Error adding generated Jar file", e);
    }
    // Copy dependencies
    try {
        Set<Artifact> artifacts = null;
        if (useTransitiveDependencies || "all".equals(archiveContent)) {
            // to the same compatible value or is the default).
            if ("none".equals(archiveContent)) {
                throw new MojoExecutionException("<useTransitiveDependencies/> and <archiveContent/> incompatibly configured.  <useTransitiveDependencies/> is deprecated in favor of <archiveContent/>.");
            } else {
                artifacts = project.getArtifacts();
            }
        } else {
            // check that archiveContent is compatible
            if ("applicationContent".equals(archiveContent)) {
                artifacts = project.getDependencyArtifacts();
            } else {
                // the only remaining options should be applicationContent="none"
                getLog().info("archiveContent=none: application arvhive will not contain any bundles.");
            }
        }
        if (artifacts != null) {
            for (Artifact artifact : artifacts) {
                ScopeArtifactFilter filter = new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME);
                if (!artifact.isOptional() && filter.include(artifact)) {
                    getLog().info("Copying artifact[" + artifact.getGroupId() + ", " + artifact.getId() + ", " + artifact.getScope() + "]");
                    zipArchiver.addFile(artifact.getFile(), artifact.getArtifactId() + "-" + artifact.getVersion() + "." + (artifact.getType() == null ? "jar" : artifact.getType()));
                }
            }
        }
    } catch (ArchiverException e) {
        throw new MojoExecutionException("Error copying EBA dependencies", e);
    }
    // Copy source files
    try {
        File ebaSourceDir = ebaSourceDirectory;
        if (ebaSourceDir.exists()) {
            getLog().info("Copy eba resources to " + getBuildDir().getAbsolutePath());
            DirectoryScanner scanner = new DirectoryScanner();
            scanner.setBasedir(ebaSourceDir.getAbsolutePath());
            scanner.setIncludes(DEFAULT_INCLUDES);
            scanner.addDefaultExcludes();
            scanner.scan();
            String[] dirs = scanner.getIncludedDirectories();
            for (int j = 0; j < dirs.length; j++) {
                new File(getBuildDir(), dirs[j]).mkdirs();
            }
            String[] files = scanner.getIncludedFiles();
            for (int j = 0; j < files.length; j++) {
                File targetFile = new File(getBuildDir(), files[j]);
                targetFile.getParentFile().mkdirs();
                File file = new File(ebaSourceDir, files[j]);
                FileUtils.copyFileToDirectory(file, targetFile.getParentFile());
            }
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Error copying EBA resources", e);
    }
    // Include custom manifest if necessary
    try {
        if (!generateManifest) {
            includeCustomApplicationManifestFile();
        }
    } catch (IOException e) {
        throw new MojoExecutionException("Error copying APPLICATION.MF file", e);
    }
    // Generate application manifest if requested
    if (generateManifest) {
        String fileName = new String(getBuildDir() + "/" + APPLICATION_MF_URI);
        File appMfFile = new File(fileName);
        try {
            // Delete any old manifest
            if (appMfFile.exists()) {
                FileUtils.fileDelete(fileName);
            }
            appMfFile.getParentFile().mkdirs();
            if (appMfFile.createNewFile()) {
                writeApplicationManifest(fileName);
            }
        } catch (java.io.IOException e) {
            throw new MojoExecutionException("Error generating APPLICATION.MF file: " + fileName, e);
        }
    }
    // Check if connector deployment descriptor is there
    File ddFile = new File(getBuildDir(), APPLICATION_MF_URI);
    if (!ddFile.exists()) {
        getLog().warn("Application manifest: " + ddFile.getAbsolutePath() + " does not exist.");
    }
    try {
        if (addMavenDescriptor) {
            if (project.getArtifact().isSnapshot()) {
                project.setVersion(project.getArtifact().getVersion());
            }
            String groupId = project.getGroupId();
            String artifactId = project.getArtifactId();
            zipArchiver.addFile(project.getFile(), "META-INF/maven/" + groupId + "/" + artifactId + "/pom.xml");
            PomPropertiesUtil pomPropertiesUtil = new PomPropertiesUtil();
            File dir = new File(project.getBuild().getDirectory(), "maven-zip-plugin");
            File pomPropertiesFile = new File(dir, "pom.properties");
            pomPropertiesUtil.createPomProperties(project, zipArchiver, pomPropertiesFile, forceCreation);
        }
        File ebaFile = new File(outputDirectory, finalName + ".eba");
        zipArchiver.setDestFile(ebaFile);
        File buildDir = getBuildDir();
        if (buildDir.isDirectory()) {
            zipArchiver.addDirectory(buildDir);
        }
        //include legal files if any
        File sharedResourcesDir = new File(sharedResources);
        if (sharedResourcesDir.isDirectory()) {
            zipArchiver.addDirectory(sharedResourcesDir);
        }
        zipArchiver.createArchive();
        project.getArtifact().setFile(ebaFile);
    } catch (Exception e) {
        throw new MojoExecutionException("Error assembling eba", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) IOException(java.io.IOException) IOException(java.io.IOException) Artifact(org.apache.maven.artifact.Artifact) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) DirectoryScanner(org.codehaus.plexus.util.DirectoryScanner) File(java.io.File) PomPropertiesUtil(org.apache.maven.archiver.PomPropertiesUtil)

Example 12 with ScopeArtifactFilter

use of org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter in project maven-plugins by apache.

the class TestUnpackDependenciesMojo method testIncludeRuntimeScope.

public void testIncludeRuntimeScope() throws Exception {
    mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
    mojo.getProject().setDependencyArtifacts(new HashSet<Artifact>());
    mojo.includeScope = "runtime";
    mojo.execute();
    ScopeArtifactFilter saf = new ScopeArtifactFilter(mojo.includeScope);
    for (Artifact artifact : (Iterable<Artifact>) mojo.getProject().getArtifacts()) {
        assertUnpacked(saf.include(artifact), artifact);
    }
}
Also used : ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) Artifact(org.apache.maven.artifact.Artifact)

Example 13 with ScopeArtifactFilter

use of org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter in project maven-plugins by apache.

the class TestUnpackDependenciesMojo method testExcludeCompileScope.

public void testExcludeCompileScope() throws Exception {
    mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
    mojo.getProject().setDependencyArtifacts(new HashSet<Artifact>());
    mojo.excludeScope = "compile";
    mojo.execute();
    ScopeArtifactFilter saf = new ScopeArtifactFilter(mojo.excludeScope);
    for (Artifact artifact : (Iterable<Artifact>) mojo.getProject().getArtifacts()) {
        assertUnpacked(!saf.include(artifact), artifact);
    }
}
Also used : ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) Artifact(org.apache.maven.artifact.Artifact)

Example 14 with ScopeArtifactFilter

use of org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter in project maven-plugins by apache.

the class TestCopyDependenciesMojo method testExcludeRuntimeScope.

public void testExcludeRuntimeScope() throws Exception {
    mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
    mojo.getProject().setDependencyArtifacts(new HashSet<Artifact>());
    mojo.excludeScope = "runtime";
    mojo.execute();
    ScopeArtifactFilter saf = new ScopeArtifactFilter(mojo.excludeScope);
    Set<Artifact> artifacts = mojo.getProject().getArtifacts();
    for (Artifact artifact : artifacts) {
        String fileName = DependencyUtil.getFormattedFileName(artifact, false);
        File file = new File(mojo.outputDirectory, fileName);
        assertEquals(!saf.include(artifact), file.exists());
    }
}
Also used : ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact)

Example 15 with ScopeArtifactFilter

use of org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter in project maven-plugins by apache.

the class TestCopyDependenciesMojo method testExcludeCompileScope.

public void testExcludeCompileScope() throws Exception {
    mojo.getProject().setArtifacts(stubFactory.getScopedArtifacts());
    mojo.getProject().setDependencyArtifacts(new HashSet<Artifact>());
    mojo.excludeScope = "compile";
    mojo.execute();
    ScopeArtifactFilter saf = new ScopeArtifactFilter(mojo.excludeScope);
    Set<Artifact> artifacts = mojo.getProject().getArtifacts();
    for (Artifact artifact : artifacts) {
        String fileName = DependencyUtil.getFormattedFileName(artifact, false);
        File file = new File(mojo.outputDirectory, fileName);
        assertEquals(!saf.include(artifact), file.exists());
    }
}
Also used : ScopeArtifactFilter(org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter) File(java.io.File) Artifact(org.apache.maven.artifact.Artifact)

Aggregations

ScopeArtifactFilter (org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter)20 Artifact (org.apache.maven.artifact.Artifact)19 File (java.io.File)8 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)2 InvalidVersionSpecificationException (org.apache.maven.artifact.versioning.InvalidVersionSpecificationException)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 MavenArchiver (org.apache.maven.archiver.MavenArchiver)1 PomPropertiesUtil (org.apache.maven.archiver.PomPropertiesUtil)1 ArtifactResolutionResult (org.apache.maven.artifact.resolver.ArtifactResolutionResult)1 PluginDescriptor (org.apache.maven.plugin.descriptor.PluginDescriptor)1 ArtifactTypeMappingService (org.apache.maven.plugins.ear.util.ArtifactTypeMappingService)1 JavaEEVersion (org.apache.maven.plugins.ear.util.JavaEEVersion)1 MavenProject (org.apache.maven.project.MavenProject)1