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

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

Introduction

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

Prototype

boolean isResolved();

Source Link

Usage

From source file:org.sonatype.flexmojos.plugin.compiler.AbstractFlexCompilerMojo.java

protected Artifact adaptResourceBundle(final Artifact baseRbSwc, String requestedLocale) {
    getLog().debug("Adapting resource bundle " + baseRbSwc.getArtifactId() + ":" + baseRbSwc.getClassifier()
            + " to " + requestedLocale);

    Artifact rbSwc;
    try {//from   www .ja  v a 2 s.c  om
        rbSwc = resolve(baseRbSwc.getGroupId(), baseRbSwc.getArtifactId(), baseRbSwc.getVersion(),
                baseRbSwc.getClassifier() + "2" + requestedLocale, baseRbSwc.getType());
    } catch (RuntimeMavenResolutionException e) {
        rbSwc = e.getArtifact();
    }

    if (rbSwc.isResolved()) {
        return rbSwc;
    }

    File dest;
    try {
        UnArchiver unzip = archiverManager.getUnArchiver("zip");
        unzip.setSourceFile(baseRbSwc.getFile());
        dest = FileUtils.createTempFile(baseRbSwc.getArtifactId(), requestedLocale, getOutputDirectory());
        unzip.extract("locale/" + baseRbSwc.getClassifier(), dest);
    } catch (Exception e) {
        throw new MavenRuntimeException("Unable to extract base locale", e);
    }

    File resourceBundleBaseDir = new File(dest, "locale/" + baseRbSwc.getClassifier());
    List<String> bundles = new ArrayList<String>();
    for (String bundle : resourceBundleBaseDir.list()) {
        bundles.add(bundle.replace(".properties", ""));
    }

    ICompcConfiguration cfg = mock(ICompcConfiguration.class, RETURNS_NULL);
    when(cfg.getLoadConfig()).thenReturn(getLoadConfig());
    when(cfg.getIncludeResourceBundles()).thenReturn(bundles);
    String output = PathUtil.path(baseRbSwc.getFile()).replace(baseRbSwc.getClassifier(),
            rbSwc.getClassifier());
    when(cfg.getOutput()).thenReturn(output);

    ICompilerConfiguration compilerCfg = mock(ICompilerConfiguration.class, RETURNS_NULL);
    when(compilerCfg.getTheme()).thenReturn(Collections.EMPTY_LIST);
    when(compilerCfg.getFontsConfiguration()).thenReturn(getFontsConfiguration());
    when(compilerCfg.getLocale()).thenReturn(new String[] { requestedLocale });
    when(compilerCfg.getSourcePath()).thenReturn(new File[] { resourceBundleBaseDir });
    when(compilerCfg.getExternalLibraryPath()).thenReturn(this.getExternalLibraryPath());
    when(compilerCfg.getLibraryPath()).thenReturn(this.getLibraryPath(false));

    when(cfg.getCompilerConfiguration()).thenReturn(compilerCfg);

    try {
        checkResult(compiler.compileSwc(cfg, true));
    } catch (Exception e) {
        throw new MavenRuntimeException("Unable to compile adapted resource bundle", e);
    }

    rbSwc.setFile(new File(output));
    rbSwc.setResolved(true);
    return rbSwc;
}

From source file:org.sonatype.flexmojos.plugin.compiler.AbstractFlexCompilerMojo.java

private Artifact doLocalizationChain(String[] locales, String requestedLocale, Artifact beacon,
        Artifact requestRbSwc) {/*from w w w  .  j ava  2 s.  c om*/
    getLog().info("Resolving resource bundle for '" + beacon + "' using localization chain.");

    for (String locale : locales) {
        Artifact rbSwc;
        try {
            rbSwc = resolve(beacon.getGroupId(), beacon.getArtifactId(), beacon.getVersion(), locale,
                    beacon.getType());
        } catch (RuntimeMavenResolutionException e) {
            rbSwc = e.getArtifact();
        }

        if (rbSwc.isResolved()) {
            if (!requestedLocale.equals(locale)) {
                getLog().info("Resolved resource bundle for '" + beacon + "' using localization chain. The '"
                        + locale + "' will be used to build the missing '" + requestedLocale + "'");
                return adaptResourceBundle(rbSwc, requestedLocale);
            }

            return rbSwc;
        }
    }

    throw new MavenRuntimeException(
            "Unable to resolve resource bundle '" + beacon + "' for '" + requestedLocale + "'");
}

From source file:org.sonatype.flexmojos.plugin.compiler.AbstractFlexCompilerMojo.java

@SuppressWarnings("unchecked")
protected Collection<Artifact> getCompiledResouceBundles() {
    if (this.getLocale() == null) {
        return null;
    }/* ww  w . j  a  va2  s .  c  om*/

    Collection<Artifact> rbsSwc = new LinkedHashSet<Artifact>();

    Set<Artifact> beacons = getDependencies(type(RB_SWC));

    String[] localeChains = this.localesCompiled;
    if (localeChains == null) {
        localeChains = getLocale();
    }

    // TODO for for for for if for for, too many nested blocks, improve this
    for (Artifact beacon : beacons) {
        for (String localeChain : localeChains) {
            String[] locales;
            if (localeChain.contains(",")) {
                locales = localeChain.split(",");
            } else {
                locales = new String[] { localeChain };
            }

            String requestedLocale = locales[0];

            Artifact requestedRbSwc;
            try {
                requestedRbSwc = resolve(beacon.getGroupId(), beacon.getArtifactId(), beacon.getVersion(),
                        requestedLocale, beacon.getType());
            } catch (RuntimeMavenResolutionException e) {
                requestedRbSwc = e.getArtifact();
            }

            Artifact resultRbSwc;
            if (requestedRbSwc.isResolved()) {
                resultRbSwc = requestedRbSwc;
            } else if (locales.length > 1) {
                resultRbSwc = doLocalizationChain(locales, requestedLocale, beacon, requestedRbSwc);
            } else {
                throw new MavenRuntimeException("Missing resource bundle '" + requestedRbSwc + "'");
            }

            rbsSwc.add(resultRbSwc);
        }
    }

    return rbsSwc;
}

From source file:org.sonatype.flexmojos.plugin.compiler.AbstractFlexCompilerMojo.java

@SuppressWarnings("unchecked")
public boolean isCompilationRequired() {
    if (!quick) {
        // not running at quick mode
        return true;
    }/*from   www.j  a va  2 s  .  co  m*/

    Artifact artifact;
    try {
        artifact = resolve(project.getGroupId(), project.getArtifactId(), project.getVersion(), getClassifier(),
                project.getPackaging());
    } catch (RuntimeMavenResolutionException e) {
        artifact = e.getArtifact();
    }

    if (!artifact.isResolved() || artifact.getFile() == null || !artifact.getFile().exists()) {
        // Recompile, file doesn't exists
        getLog().warn("Can't find any older installed version.");
        return true;
    }

    long lastCompiledArtifact = artifact.getFile().lastModified();

    boolean required = false;
    Set<Artifact> dependencies = getDependencies();
    for (Artifact dependency : dependencies) {
        if (org.apache.commons.io.FileUtils.isFileNewer(dependency.getFile(), lastCompiledArtifact)) {
            // a dependency is newer, recompile
            getLog().warn("Found a updated dependency: " + dependency);
            required = true;
        }
    }

    if (!required) {
        Collection<File> files = org.apache.commons.io.FileUtils.listFiles(
                new File(project.getBuild().getSourceDirectory()),
                new AgeFileFilter(lastCompiledArtifact, false), TrueFileFilter.INSTANCE);

        // If has any newer file
        if (files.size() > 0) {
            getLog().warn("Found some updated files.");
            required = true;
        }
    }

    if (!required) {
        try {
            final File output = new File(getOutput());

            FileUtils.copyFile(artifact.getFile(), output);

            if (!output.setLastModified(artifact.getFile().lastModified())) {
                getLog().warn("Could not set modified on copied artifact. Unnecessary rebuilds will occur.");
            }
        } catch (IOException e) {
            getLog().error("Unable to copy installed version to target folder.", e);
            return true;
        }
    }

    // nothing new was found.
    return required;
}

From source file:org.sonatype.flexmojos.plugin.htmlwrapper.HtmlWrapperMojo.java

License:Open Source License

/**
 * Attempts to find the provided artifact within the current repository path
 * //from  ww  w. jav  a 2s.  c  om
 * @param artifact
 * @throws MojoExecutionException
 */
private void resolveArtifact(Artifact artifact) throws MojoExecutionException {
    if (!artifact.isResolved()) {
        ArtifactResolutionRequest req = new ArtifactResolutionRequest();
        req.setArtifact(artifact);
        req.setLocalRepository(localRepository);
        req.setRemoteRepositories(remoteRepositories);
        // FIXME need to check isSuccess
        repositorySystem.resolve(req).isSuccess();
    }
}

From source file:org.sonatype.flexmojos.plugin.utilities.MavenUtils.java

License:Open Source License

public static Set<File> getFilesSet(Collection<Artifact>... dependenciesSet) {
    if (dependenciesSet == null) {
        return null;
    }//ww w .  ja  v a2  s. c  om

    Set<File> files = new LinkedHashSet<File>();
    for (Collection<Artifact> dependencies : dependenciesSet) {
        if (dependencies == null) {
            continue;
        }

        for (Artifact artifact : dependencies) {
            if (!artifact.isResolved()) {
                throw new IllegalArgumentException("Unable to handle unresolved artifact: " + artifact);
            }

            if (artifact.getFile() == null) {
                throw new NullPointerException("Artifact file not defined: " + artifact);
            }

            files.add(artifact.getFile());
        }
    }
    return files;
}

From source file:org.sonatype.flexmojos.rsl.InstallerMojo.java

License:Apache License

/**
 * @param artifact//from  w ww . j  a v a  2  s.c  o m
 * @param rslArtifact
 * @throws MojoExecutionException
 */
protected void processDependency(Artifact artifact, Artifact rslArtifact) throws MojoExecutionException {

    try {
        // lookup RSL artifact
        resolver.resolve(rslArtifact, remoteRepositories, localRepository);
        getLog().debug("Artifact RSL found: " + rslArtifact);
        File outputFile = new File(outputDirectory, getFormattedFileName(rslArtifact));
        try {
            FileUtils.copyFile(rslArtifact.getFile(), outputFile);
        } catch (IOException ioe) {
            throw new MojoExecutionException(ioe.getMessage(), ioe);
        }
    } catch (AbstractArtifactResolutionException aare) {
        // RSL not available then create it
        ZipFile archive = null;
        InputStream input = null;
        OutputStream output = null;
        File originalFile = null;
        try {
            Artifact originalArtifact = artifactFactory.createArtifactWithClassifier(artifact.getGroupId(),
                    artifact.getArtifactId(), artifact.getVersion(), artifact.getType(), originalClassifier);
            try {
                resolver.resolve(originalArtifact, remoteRepositories, localRepository);
                artifact = originalArtifact;
                getLog().debug("Original artifact found: the RSL will be produced against the original one");
            } catch (Exception e) {
                getLog().debug(
                        "Original artifact not found: assuming the RSL production has never been executed before");
                resolver.resolve(artifact, remoteRepositories, localRepository);
            }
            archive = newZipFile(artifact.getFile());
            input = readLibrarySwf(artifact.getFile(), archive);
            File outputFile = new File(outputDirectory, getFormattedFileName(rslArtifact));
            output = new FileOutputStream(outputFile);

            if (optimizeRsls) {
                originalFile = new File(project.getBuild().getOutputDirectory(),
                        originalArtifact.getFile().getName());
                FileUtils.copyFile(artifact.getFile(), originalFile);
                getLog().info("Attempting to optimize: " + artifact);
                long initialSize = artifact.getFile().length() / 1024;
                optimize(input, output);
                long optimizedSize = outputFile.length() / 1024;
                getLog().info("\t\tsize reduced from " + initialSize + "kB to " + optimizedSize + "kB");

                updateDigest(outputFile, artifact.getFile());
            }
            if (deploy) {
                ArtifactRepository deploymentRepository = project.getDistributionManagementArtifactRepository();
                if (optimizeRsls) {
                    if (backup) {
                        deployer.deploy(originalFile, originalArtifact, deploymentRepository, localRepository);
                    }
                    deployer.deploy(artifact.getFile(), artifact, deploymentRepository, localRepository);
                }
                deployer.deploy(outputFile, rslArtifact, deploymentRepository, localRepository);
            } else {
                if (backup && optimizeRsls && !originalArtifact.isResolved()) {
                    try {
                        resolver.resolve(originalArtifact, remoteRepositories, localRepository);
                    } catch (Exception e) {
                        installer.install(originalFile, originalArtifact, localRepository);
                    }
                }
                installer.install(outputFile, rslArtifact, localRepository);
            }
        } catch (Exception e) {
            throw new MojoExecutionException(e.getMessage(), e);
        } finally {
            IOUtil.close(input);
            IOUtil.close(output);
            if (archive != null) {
                try {
                    archive.close();
                    if (originalFile != null) {
                        originalFile.delete();
                    }
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    }
}

From source file:org.sonatype.flexmojos.utilities.MavenUtils.java

License:Apache License

/**
 * Use the resolver to resolve the given artifact in the local or remote repositories.
 * /*from   w  w  w .  jav a  2  s  . co  m*/
 * @param project Active project
 * @param artifact Artifact to be resolved
 * @param resolver ArtifactResolver to use for resolving the artifact
 * @param localRepository ArtifactRepository
 * @param remoteRepositories List of remote artifact repositories
 * @throws MojoExecutionException thrown if an exception occured during artifact resolving
 * @return resolved artifact
 */
@SuppressWarnings("unchecked")
public static Artifact resolveArtifact(MavenProject project, Artifact artifact, ArtifactResolver resolver,
        ArtifactRepository localRepository, List remoteRepositories) throws MojoExecutionException {
    try {
        artifact = project.replaceWithActiveArtifact(artifact);
        if (!artifact.isResolved()) {
            resolver.resolve(artifact, remoteRepositories, localRepository);
        }
        return artifact;
    } catch (AbstractArtifactResolutionException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}

From source file:org.sonatype.flexmojos.utilities.MavenUtils.java

License:Apache License

public static Set<File> getFilesSet(Collection<Artifact> dependencies) {
    if (dependencies == null) {
        return null;
    }/*from  w w  w.  j ava 2 s .c  o  m*/

    Set<File> files = new LinkedHashSet<File>();
    for (Artifact artifact : dependencies) {
        if (!artifact.isResolved()) {
            throw new IllegalArgumentException("Unable to handle unresolved artifact: " + artifact);
        }

        if (artifact.getFile() == null) {
            throw new NullPointerException("Artifact file not defined: " + artifact);
        }

        files.add(artifact.getFile());
    }
    return files;
}

From source file:org.sonatype.plugin.nexus.testenvironment.AbstractEnvironmentMojo.java

License:Open Source License

protected Artifact resolve(Artifact artifact) throws MojoExecutionException {
    if (!artifact.isResolved()) {
        try {//from   www  . j ava2 s  .  co  m
            resolver.resolve(artifact, remoteRepositories, localRepository);
        } catch (AbstractArtifactResolutionException e) {
            throw new MojoExecutionException("Unable to resolve artifact: " + artifact, e);
        }
    }

    return artifact;
}