Example usage for org.apache.maven.artifact Artifact isOptional

List of usage examples for org.apache.maven.artifact Artifact isOptional

Introduction

In this page you can find the example usage for org.apache.maven.artifact Artifact isOptional.

Prototype

boolean isOptional();

Source Link

Usage

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

License:Apache License

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

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

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

License:Apache License

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

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

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

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

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

From source file:cn.wanghaomiao.maven.plugin.seimi.util.WarUtils.java

License:Apache License

/**
 * @param artifact {@link Artifact}/*  w w  w .  jav  a 2  s. c o  m*/
 * @param dependency {@link Dependency}
 * @return is related or not.
 */
public static boolean isRelated(Artifact artifact, Dependency dependency) {
    if (artifact == null || dependency == null) {
        return false;
    }

    if (!StringUtils.equals(artifact.getGroupId(), dependency.getGroupId())) {
        return false;
    }
    if (!StringUtils.equals(artifact.getArtifactId(), dependency.getArtifactId())) {
        return false;
    }
    if (artifact.getVersion() != null ? !artifact.getVersion().equals(dependency.getVersion())
            : dependency.getVersion() != null) {
        return false;
    }
    if (artifact.getType() != null ? !artifact.getType().equals(dependency.getType())
            : dependency.getType() != null) {
        return false;
    }
    if (artifact.getClassifier() != null ? !artifact.getClassifier().equals(dependency.getClassifier())
            : dependency.getClassifier() != null) {
        return false;
    }
    if (artifact.getScope() != null ? !artifact.getScope().equals(dependency.getScope())
            : dependency.getScope() != null) {
        return false;
    }
    if (artifact.isOptional() != dependency.isOptional()) {
        return false;
    }

    return true;
}

From source file:com.adviser.maven.GraphArtifactCollector.java

License:Apache License

public ArtifactResolutionResult collect(Set<Artifact> artifacts, Artifact originatingArtifact,
        Map managedVersions, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories,
        ArtifactMetadataSource source, ArtifactFilter filter, List<ResolutionListener> listeners) {
    Map resolvedArtifacts = new HashMap();

    ResolutionNode root = new ResolutionNode(originatingArtifact, remoteRepositories);
    try {/*from  w ww.j a  v a 2s  . c om*/
        root.addDependencies(artifacts, remoteRepositories, filter);
        recurse(root, resolvedArtifacts, managedVersions, localRepository, remoteRepositories, source, filter,
                listeners);
    } catch (CyclicDependencyException e) {
        e.printStackTrace();
    } catch (OverConstrainedVersionException e) {
        e.printStackTrace();
    } catch (ArtifactResolutionException e) {
        e.printStackTrace();
    }

    Set set = new HashSet();
    for (Iterator i = resolvedArtifacts.values().iterator(); i.hasNext();) {
        List nodes = (List) i.next();
        for (Iterator j = nodes.iterator(); j.hasNext();) {
            ResolutionNode node = (ResolutionNode) j.next();
            Artifact artifact = node.getArtifact();
            try {
                if (!node.equals(root) && node.isActive() && node.filterTrail(filter)
                // If it was optional and not a direct dependency,
                // we don't add it or its children, just allow the
                // update of the version and scope
                        && (node.isChildOfRootNode() || !artifact.isOptional())) {
                    artifact.setDependencyTrail(node.getDependencyTrail());
                    set.add(node);
                }
            } catch (OverConstrainedVersionException e) {
                e.printStackTrace();
            }
        }
    }

    ArtifactResolutionResult result = new ArtifactResolutionResult();
    result.setArtifactResolutionNodes(set);
    return result;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

From source file:com.carrotgarden.maven.osgi.BaseMojo.java

License:BSD License

/**
 *///from ww w.j a va2 s.  c o  m
protected void addProjectDependencies(MavenProject project, String tab) {

    // getLog().warn("addProjectDependencies : " + project);

    @SuppressWarnings("unchecked")
    Set<Artifact> artifacts = project.getArtifacts();

    // getLog().warn("artifacts.size : " + artifacts.size());

    for (Artifact artifact : artifacts) {

        if (artifact.isOptional()) {
            continue;
        }

        if (Artifact.SCOPE_TEST.equals(artifact.getScope())) {
            continue;
        }

        provisionBundle(artifact, tab);

    }

}

From source file:com.carrotgarden.maven.osgi.MakeArchiveMojo.java

License:BSD License

protected boolean isIncluded(Artifact artifact) {
    if (artifact.isOptional() && !includeOptional) {
        return false;
    }/*w  w  w.j a  va 2s  . co  m*/
    if (!ArtifactScope.isIncluded(includeScopeList, artifact)) {
        return false;
    }
    return true;
}

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

License:Apache License

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

    for (final Artifact artifact : artifacts) {
        if (filter.include(artifact) && !artifact.isOptional()) {
            result.add(artifact);/*  w w w  . ja v  a 2s . c  o  m*/
        }
    }

    return result;
}

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

License:Apache License

private Set<Artifact> getNotProvidedDependencies() throws Exception {
    final Set<Artifact> result = new HashSet<Artifact>();
    for (final Artifact artifact : getIncludedArtifacts()) {
        if (Artifact.SCOPE_PROVIDED.equals(artifact.getScope())
                || Artifact.SCOPE_TEST.equals(artifact.getScope())) {
            continue;
        }/* w w w .j  av  a 2s  .  c  o  m*/

        if (artifact.isOptional()) {
            continue;
        }

        result.add(artifact);
    }

    return result;
}

From source file:com.github.maven_nar.AttachedNarArtifact.java

License:Apache License

public AttachedNarArtifact(final Artifact parent, final String type, final String classifier) {
    super(parent.getGroupId(), parent.getArtifactId(), parent.getVersionRange(), parent.getScope(), type,
            classifier, null, parent.isOptional());
    setArtifactHandler(new Handler(classifier));
}