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

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

Introduction

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

Prototype

String SCOPE_RUNTIME

To view the source code for org.apache.maven.artifact Artifact SCOPE_RUNTIME.

Click Source Link

Usage

From source file:at.yawk.mdep.GenerateMojo.java

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (cacheHours > 0) {
        cacheStore = Environment.createCacheStore(new Logger() {
            @Override// w  w w.j a  va  2s  .  c  om
            public void info(String msg) {
                getLog().info(msg);
            }

            @Override
            public void warn(String msg) {
                getLog().warn(msg);
            }
        }, "mdep-maven-plugin");
    }

    ArtifactMatcher includesMatcher;
    if (includes == null) {
        includesMatcher = ArtifactMatcher.acceptAll();
    } else {
        includesMatcher = ArtifactMatcher.anyMatch(toAntMatchers(includes));
    }
    ArtifactMatcher excludesMatcher = ArtifactMatcher.anyMatch(toAntMatchers(excludes));
    ArtifactMatcher matcher = includesMatcher.and(excludesMatcher.not());

    List<Artifact> artifacts = new ArrayList<>();

    try {
        ArtifactFilter subtreeFilter = artifact -> artifact.getScope() == null
                || artifact.getScope().equals(Artifact.SCOPE_COMPILE)
                || artifact.getScope().equals(Artifact.SCOPE_RUNTIME);
        DependencyNode root = dependencyTreeBuilder.buildDependencyTree(project, localRepository,
                subtreeFilter);
        root.accept(new DependencyNodeVisitor() {
            @Override
            public boolean visit(DependencyNode node) {
                if (node.getArtifact() != null) {
                    if (!subtreeFilter.include(node.getArtifact())) {
                        return false;
                    }
                    artifacts.add(node.getArtifact());
                }
                return true;
            }

            @Override
            public boolean endVisit(DependencyNode node) {
                return true;
            }
        });
    } catch (DependencyTreeBuilderException e) {
        throw new MojoExecutionException("Failed to build dependency tree", e);
    }

    List<Dependency> dependencies = new ArrayList<>();
    for (Artifact artifact : artifacts) {
        if (matcher.matches(artifact)) {
            dependencies.add(findArtifact(artifact));
        }
    }

    getLog().info("Saving dependency xml");

    DependencySet dependencySet = new DependencySet();
    dependencySet.setDependencies(dependencies);

    if (!outputDirectory.mkdirs()) {
        throw new MojoExecutionException("Failed to create output directory");
    }
    File outputFile = new File(outputDirectory, "mdep-dependencies.xml");

    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(DependencySet.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.marshal(dependencySet, outputFile);

    } catch (JAXBException e) {
        throw new MojoExecutionException("Failed to serialize dependency set", e);
    }
    Resource resource = new Resource();
    resource.setDirectory(outputDirectory.toString());
    resource.setFiltering(false);
    project.addResource(resource);
}

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

License:Apache License

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

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

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

License:Apache License

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

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

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

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

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

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

License:Apache License

private void checkScopeUpdate(ResolutionNode farthest, ResolutionNode nearest, List listeners) {
    boolean updateScope = false;
    Artifact farthestArtifact = farthest.getArtifact();
    Artifact nearestArtifact = nearest.getArtifact();

    if (Artifact.SCOPE_RUNTIME.equals(farthestArtifact.getScope())
            && (Artifact.SCOPE_TEST.equals(nearestArtifact.getScope())
                    || Artifact.SCOPE_PROVIDED.equals(nearestArtifact.getScope()))) {
        updateScope = true;/*from   w ww .  j  a v  a 2s. c  o m*/
    }

    if (Artifact.SCOPE_COMPILE.equals(farthestArtifact.getScope())
            && !Artifact.SCOPE_COMPILE.equals(nearestArtifact.getScope())) {
        updateScope = true;
    }

    // current POM rules all
    if (nearest.getDepth() < 2 && updateScope) {
        updateScope = false;

        fireEvent(ResolutionListener.UPDATE_SCOPE_CURRENT_POM, listeners, nearest, farthestArtifact);
    }

    if (updateScope) {
        fireEvent(ResolutionListener.UPDATE_SCOPE, listeners, nearest, farthestArtifact);

        // previously we cloned the artifact, but it is more effecient to
        // just update the scope
        // if problems are later discovered that the original object needs
        // its original scope value, cloning may
        // again be appropriate
        nearestArtifact.setScope(farthestArtifact.getScope());
    }
}

From source file:com.akathist.maven.plugins.launch4j.ClassPath.java

License:Open Source License

net.sf.launch4j.config.ClassPath toL4j(Set dependencies) {
    net.sf.launch4j.config.ClassPath ret = new net.sf.launch4j.config.ClassPath();
    ret.setMainClass(mainClass);/*from   ww  w  .  j  a  va  2s  .co  m*/

    List cp = new ArrayList();
    if (preCp != null)
        addToCp(cp, preCp);

    if (addDependencies) {
        if (jarLocation == null)
            jarLocation = "";
        else if (!jarLocation.endsWith("/"))
            jarLocation += "/";

        Iterator i = dependencies.iterator();
        while (i.hasNext()) {
            Artifact dep = (Artifact) i.next();
            if (Artifact.SCOPE_COMPILE.equals(dep.getScope())
                    || Artifact.SCOPE_RUNTIME.equals(dep.getScope())) {

                String depFilename;
                depFilename = dep.getFile().getName();
                // System.out.println("dep = " + depFilename);
                cp.add(jarLocation + depFilename);
            }
        }
    }

    if (postCp != null)
        addToCp(cp, postCp);
    ret.setPaths(cp);

    return ret;
}

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]+)*\"");
    }/*  w ww  .  j  a  v a 2s  .co m*/
    if (!projectVersion.equals(finalVersion)) {
        getLog().info("Filtering version to have a valid alfresco version");
    }
    getLog().info("Using final version " + finalVersion);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

License:Open Source License

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

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

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

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

License:Apache License

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

    for (final Artifact artifact : artifacts) {
        if (filter.include(artifact) && !artifact.isOptional()) {
            result.add(artifact);//w w  w  . j  a  va  2  s.  c  om
        }
    }

    return result;
}

From source file:com.fizzed.stork.maven.AssemblyMojo.java

public List<Artifact> artifactsToStage() {
    List<Artifact> artifacts = new ArrayList<Artifact>();

    // include project artifact?
    if (!shouldArtifactBeStaged(project.getArtifact())) {
        getLog().info("Project artifact may have a classifier or is not of type jar (will not be staged)");
    } else {/*from www. ja  v  a 2  s  .c o m*/
        artifacts.add(project.getArtifact());
    }

    // any additional artifacts attached to this project?
    for (Artifact a : project.getAttachedArtifacts()) {
        if (shouldArtifactBeStaged(a)) {
            artifacts.add(a);
        }
    }

    // get resolved artifacts as well
    for (Artifact a : project.getArtifacts()) {
        if (a.getArtifactHandler().isAddedToClasspath() && (Artifact.SCOPE_COMPILE.equals(a.getScope())
                || Artifact.SCOPE_RUNTIME.equals(a.getScope()))) {
            artifacts.add(a);
        }
    }

    return artifacts;
}

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

License:Apache License

/**
 * List the dependencies we want to assemble
 *///w ww .jav  a2  s .c om
@Override
protected ScopeFilter getArtifactScopeFilter() {
    // Was Artifact.SCOPE_RUNTIME  + provided?
    // Think Provided isn't appropriate in Assembly - otherwise it isn't provided.
    return new ScopeFilter(Artifact.SCOPE_RUNTIME, null);
}