Example usage for org.apache.maven.artifact.repository ArtifactRepository pathOf

List of usage examples for org.apache.maven.artifact.repository ArtifactRepository pathOf

Introduction

In this page you can find the example usage for org.apache.maven.artifact.repository ArtifactRepository pathOf.

Prototype

String pathOf(Artifact artifact);

Source Link

Usage

From source file:com.alibaba.citrus.maven.eclipse.base.ide.IdeUtils.java

License:Apache License

/**
 * @param artifact the artifact//from  w  w  w  .  j  av  a2 s . c o m
 * @return the not-available marker file for the specified artifact
 */
public static File getNotAvailableMarkerFile(ArtifactRepository localRepository, Artifact artifact) {
    return new File(localRepository.getBasedir(),
            localRepository.pathOf(artifact) + NOT_AVAILABLE_MARKER_FILE_SUFFIX);
}

From source file:com.googlecode.jndiresources.maven.ManageArtifact.java

License:Apache License

/**
 * Load the artifact in local repository, and return the file path.
 * //from ww w  .j a v a  2 s .com
 * @param groupId The Maven group id.
 * @param artifactId The Maven artifact id.
 * @param version The Maven version.
 * @param repositories A list of String with remoteRepositories or null.
 * @return The path in local repository.
 * @throws MavenException If error when dowload or identify the maven jar
 *             file.
 * @throws ArtifactNotFoundException If artifact is not found.
 * @throws ResourceDoesNotExistException If error.
 */
public static String getArtifact(final String groupId, final String artifactId, final String version,
        final List repositories)
        throws MavenException, ArtifactNotFoundException, ResourceDoesNotExistException {
    // TODO : rcuperer la bonne version en cas de suprieur...
    try {
        if (maven_ == null) {
            maven_ = new MavenEmbedder();
            maven_.setClassLoader(Thread.currentThread().getContextClassLoader());
            maven_.setAlignWithUserInstallation(true);
            maven_.start();
        }

        final String scope = Artifact.RELEASE_VERSION;
        final String type = "jar";
        final ArtifactRepository localRepository = maven_.getLocalRepository();

        final Artifact artifact = maven_.createArtifact(groupId, artifactId, version, scope, type);
        final List remoteRepositories = new ArrayList();

        if ((repositories == null) || repositories.isEmpty())
            remoteRepositories.add(maven_.createRepository(CENTRAL_REPO, "central"));
        else {
            int id = 0;
            for (final Iterator i = repositories.iterator(); i.hasNext();) {
                remoteRepositories.add(maven_.createRepository((String) i.next(), "central_" + id++));
            }
        }
        maven_.resolve(artifact, remoteRepositories, localRepository);
        return localRepository.getBasedir() + File.separatorChar + localRepository.pathOf(artifact);
    } catch (ArtifactNotFoundException e) {
        throw (ArtifactNotFoundException) e.fillInStackTrace();
    } catch (Exception e) {
        throw new MavenException("Repository error", e);
    }
}

From source file:fr.imag.adele.cadse.platform.DependenciesTask.java

License:Apache License

private void addArtifactToResult(ArtifactRepository localRepo, Artifact artifact, FileSet toFileSet,
        Path path) {//from ww  w.  j a  v a 2s  . c o  m
    String filename = localRepo.pathOf(artifact);

    toFileSet.createInclude().setName(filename);

    getProject().setProperty(artifact.getDependencyConflictId(), artifact.getFile().getAbsolutePath());

    FileSet artifactFileSet = new FileSet();
    artifactFileSet.setProject(getProject());
    artifactFileSet.setFile(artifact.getFile());
    getProject().addReference(artifact.getDependencyConflictId(), artifactFileSet);

    if (path != null) {
        path.addFileset(artifactFileSet);
    }
}

From source file:hudson.gridmaven.reporters.MavenFingerprinter.java

License:Open Source License

private void recordParents(MavenBuildProxy build, MavenProject pom) throws IOException, InterruptedException {
    MavenProject parent = pom.getParent();
    while (parent != null) {
        File parentFile = parent.getFile();

        if (parentFile == null) {
            // Parent artifact contains no actual file, so we resolve against
            // the local repository
            ArtifactRepository localRepository = getLocalRepository(build.getMavenBuildInformation(), parent,
                    pom);/*from   w w w  .j  av  a  2  s.  c  o  m*/
            if (localRepository != null) {
                Artifact parentArtifact = getArtifact(parent);
                // Don't use ArtifactRepository.find(), for compatibility with Maven 2.x
                if (parentArtifact != null) {
                    parentFile = new File(localRepository.getBasedir(), localRepository.pathOf(parentArtifact));
                }
            }
        }

        if (parentFile != null) {
            // we need to include the artifact Id for poms as well, otherwise a
            // project with the same groupId would override its parent's
            // fingerprint
            record(parent.getGroupId() + ":" + parent.getArtifactId(), parentFile, used);
        }
        parent = parent.getParent();
    }
}

From source file:hudson.maven.artifact.AbstractArtifactComponentTestCase.java

License:Apache License

protected void assertRemoteArtifactPresent(Artifact artifact) throws Exception {
    ArtifactRepository remoteRepo = remoteRepository();

    String path = remoteRepo.pathOf(artifact);

    File file = new File(remoteRepo.getBasedir(), path);

    file = new File(file.getParentFile(), "artifact-1.0-SNAPSHOT.jar");

    if (!file.exists()) {
        fail("Remote artifact " + file + " should be present.");
    }// w  ww.  ja  v a  2 s.  co  m
}

From source file:hudson.maven.artifact.deployer.DefaultArtifactDeployer.java

License:Apache License

public void deploy(File source, Artifact artifact, ArtifactRepository deploymentRepository,
        ArtifactRepository localRepository) throws ArtifactDeploymentException {

    // If we're installing the POM, we need to transform it first. The source file supplied for 
    // installation here may be the POM, but that POM may not be set as the file of the supplied
    // artifact. Since the transformation only has access to the artifact and not the supplied
    // source file, we have to use the Artifact.setFile(..) and Artifact.getFile(..) methods
    // to shunt the POM file into the transformation process.
    // Here, we also set a flag indicating that the POM has been shunted through the Artifact,
    // and to expect the transformed version to be available in the Artifact afterwards...
    boolean useArtifactFile = false;
    File oldArtifactFile = artifact.getFile();
    if ("pom".equals(artifact.getType())) {
        artifact.setFile(source);/*from ww w. j  a  v  a 2s. c  o m*/
        useArtifactFile = true;
    }

    try {
        transformationManager.transformForDeployment(artifact, deploymentRepository, localRepository);

        // If we used the Artifact shunt to transform a POM source file, we need to install
        // the transformed version, not the supplied version. Therefore, we need to replace
        // the supplied source POM with the one from Artifact.getFile(..).
        if (useArtifactFile) {
            source = artifact.getFile();
            artifact.setFile(oldArtifactFile);
        }

        // FIXME: Why oh why are we re-installing the artifact in the local repository? Isn't this
        // the responsibility of the ArtifactInstaller??

        // Copy the original file to the new one if it was transformed
        File artifactFile = new File(localRepository.getBasedir(), localRepository.pathOf(artifact));
        if (!artifactFile.equals(source)) {
            FileUtils.copyFile(source, artifactFile);
        }
        //FIXME find a way to get hudson BuildListener ??
        TransferListener downloadMonitor = new TransferListener() {

            public void transferInitiated(TransferEvent transferEvent) {
                String message = transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "Uploading"
                        : "Downloading";

                String url = transferEvent.getWagon().getRepository().getUrl();

                // TODO: can't use getLogger() because this isn't currently instantiated as a component
                System.out.println(message + ": " + url + "/" + transferEvent.getResource().getName());
            }

            public void transferStarted(TransferEvent transferEvent) {
                // no op
            }

            public void transferProgress(TransferEvent transferEvent, byte[] buffer, int length) {
                // no op
            }

            public void transferCompleted(TransferEvent transferEvent) {
                long contentLength = transferEvent.getResource().getContentLength();
                if (contentLength != WagonConstants.UNKNOWN_LENGTH) {
                    String type = (transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "uploaded"
                            : "downloaded");
                    String l = contentLength >= 1024 ? (contentLength / 1024) + "K" : contentLength + "b";
                    System.out.println(l + " " + type);
                }

            }

            public void transferError(TransferEvent transferEvent) {
                transferEvent.getException().printStackTrace();
            }

            public void debug(String message) {
                // TODO Auto-generated method stub

            }

        };
        wagonManager.putArtifact(source, artifact, deploymentRepository, downloadMonitor);

        // must be after the artifact is installed
        for (Iterator i = artifact.getMetadataList().iterator(); i.hasNext();) {
            ArtifactMetadata metadata = (ArtifactMetadata) i.next();
            repositoryMetadataManager.deploy(metadata, localRepository, deploymentRepository);
        }
        // TODO: would like to flush this, but the plugin metadata is added in advance, not as an install/deploy transformation
        // This would avoid the need to merge and clear out the state during deployment
        //            artifact.getMetadataList().clear();
    } catch (TransferFailedException e) {
        throw new ArtifactDeploymentException("Error deploying artifact: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new ArtifactDeploymentException("Error deploying artifact: " + e.getMessage(), e);
    } catch (RepositoryMetadataDeploymentException e) {
        throw new ArtifactDeploymentException("Error installing artifact's metadata: " + e.getMessage(), e);
    }
}

From source file:hudson.maven.reporters.MavenFingerprinter.java

License:Open Source License

private void recordParents(MavenBuildProxy build, MavenProject pom, BuildListener listener)
        throws IOException, InterruptedException {
    Map<String, String> modelParents = build.getMavenBuildInformation().modelParents;
    ArtifactRepository localRepository = getLocalRepository(build.getMavenBuildInformation(), pom);
    if (localRepository == null) {
        listener.error(//from   ww  w  . j a  v a 2s. co m
                "Could not find local repository for " + build.getMavenBuildInformation().getMavenVersion());
        return;
    }
    String parent = modelParents.get(pom.getId());
    while (parent != null) {
        String[] parts = parent.split(":");
        assert parts.length == 4 : parent;
        // Maven 2.x lacks DefaultArtifact constructor with String version and ArtifactRepository.find:
        Artifact parentArtifact = new DefaultArtifact(parts[0], parts[1],
                VersionRange.createFromVersion(parts[3]), null, parts[2], null,
                new DefaultArtifactHandler(parts[2]));
        File parentFile = new File(localRepository.getBasedir(), localRepository.pathOf(parentArtifact));
        // we need to include the artifact Id for poms as well, otherwise a project with the same groupId would override its parent's fingerprint
        record(parts[0] + ":" + parts[1], parentFile, used);
        parent = modelParents.get(parent);
    }
}

From source file:io.fabric8.maven.docker.assembly.MappingTrackArchiver.java

License:Apache License

private File getLocalMavenRepoFile(MavenSession session, File source) {
    ArtifactRepository localRepo = session.getLocalRepository();
    if (localRepo == null) {
        log.warn("No local repo found so not adding any extra watches in the local repository");
        return null;
    }//w ww  . j  av  a2  s.c o  m

    Artifact artifact = getArtifactFromJar(source);
    if (artifact != null) {
        try {
            return new File(localRepo.getBasedir(), localRepo.pathOf(artifact));
        } catch (InvalidArtifactRTException e) {
            log.warn("Cannot get the local repository path for %s in base dir %s : %s", artifact,
                    localRepo.getBasedir(), e.getMessage());
        }
    }
    return null;
}

From source file:org.apache.archiva.converter.artifact.LegacyToDefaultConverter.java

License:Apache License

@SuppressWarnings("unchecked")
private boolean copyPom(Artifact artifact, ArtifactRepository targetRepository, FileTransaction transaction)
        throws ArtifactConversionException {
    Artifact pom = artifactFactory.createProjectArtifact(artifact.getGroupId(), artifact.getArtifactId(),
            artifact.getVersion());/*from  ww  w.j a  va2s.  c o m*/
    pom.setBaseVersion(artifact.getBaseVersion());
    ArtifactRepository repository = artifact.getRepository();
    File file = new File(repository.getBasedir(), repository.pathOf(pom));

    boolean result = true;
    if (file.exists()) {
        File targetFile = new File(targetRepository.getBasedir(), targetRepository.pathOf(pom));

        String contents = null;
        boolean checksumsValid = false;
        try {
            if (testChecksums(artifact, file)) {
                checksumsValid = true;
            }

            // Even if the checksums for the POM are invalid we should still convert the POM
            contents = FileUtils.readFileToString(file, Charset.defaultCharset());
        } catch (IOException e) {
            throw new ArtifactConversionException(
                    Messages.getString("unable.to.read.source.pom", e.getMessage()), e); //$NON-NLS-1$
        }

        if (checksumsValid && contents.indexOf("modelVersion") >= 0) //$NON-NLS-1$
        {
            // v4 POM
            try {
                boolean matching = false;
                if (!force && targetFile.exists()) {
                    String targetContents = FileUtils.readFileToString(targetFile, Charset.defaultCharset());
                    matching = targetContents.equals(contents);
                }
                if (force || !matching) {
                    transaction.createFile(contents, targetFile, digesters);
                }
            } catch (IOException e) {
                throw new ArtifactConversionException(
                        Messages.getString("unable.to.write.target.pom", e.getMessage()), e); //$NON-NLS-1$
            }
        } else {
            // v3 POM
            try (StringReader stringReader = new StringReader(contents)) {

                try (StringWriter writer = new StringWriter()) {
                    org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader v3Reader = new org.apache.maven.model.v3_0_0.io.xpp3.MavenXpp3Reader();
                    org.apache.maven.model.v3_0_0.Model v3Model = v3Reader.read(stringReader);

                    if (doRelocation(artifact, v3Model, targetRepository, transaction)) {
                        Artifact relocatedPom = artifactFactory.createProjectArtifact(artifact.getGroupId(),
                                artifact.getArtifactId(), artifact.getVersion());
                        targetFile = new File(targetRepository.getBasedir(),
                                targetRepository.pathOf(relocatedPom));
                    }

                    Model v4Model = translator.translate(v3Model);

                    translator.validateV4Basics(v4Model, v3Model.getGroupId(), v3Model.getArtifactId(),
                            v3Model.getVersion(), v3Model.getPackage());

                    MavenXpp3Writer xpp3Writer = new MavenXpp3Writer();
                    xpp3Writer.write(writer, v4Model);

                    transaction.createFile(writer.toString(), targetFile, digesters);

                    List<String> warnings = translator.getWarnings();

                    for (String message : warnings) {
                        addWarning(artifact, message);
                    }
                } catch (XmlPullParserException e) {
                    addWarning(artifact, Messages.getString("invalid.source.pom", e.getMessage())); //$NON-NLS-1$
                    result = false;
                } catch (IOException e) {
                    throw new ArtifactConversionException(Messages.getString("unable.to.write.converted.pom"),
                            e); //$NON-NLS-1$
                } catch (PomTranslationException e) {
                    addWarning(artifact, Messages.getString("invalid.source.pom", e.getMessage())); //$NON-NLS-1$
                    result = false;
                }
            }
        }
    } else {
        addWarning(artifact, Messages.getString("warning.missing.pom")); //$NON-NLS-1$
    }
    return result;
}

From source file:org.apache.archiva.converter.artifact.LegacyToDefaultConverter.java

License:Apache License

private boolean copyArtifact(Artifact artifact, ArtifactRepository targetRepository,
        FileTransaction transaction) throws ArtifactConversionException {
    File sourceFile = artifact.getFile();

    if (sourceFile.getAbsolutePath().indexOf("/plugins/") > -1) //$NON-NLS-1$
    {// w  ww. j  ava2s  .c om
        artifact.setArtifactHandler(artifactHandlerManager.getArtifactHandler("maven-plugin")); //$NON-NLS-1$
    }

    File targetFile = new File(targetRepository.getBasedir(), targetRepository.pathOf(artifact));

    boolean result = true;
    try {
        boolean matching = false;
        if (!force && targetFile.exists()) {
            matching = FileUtils.contentEquals(sourceFile, targetFile);
            if (!matching) {
                addWarning(artifact, Messages.getString("failure.target.already.exists")); //$NON-NLS-1$
                result = false;
            }
        }
        if (result) {
            if (force || !matching) {
                if (testChecksums(artifact, sourceFile)) {
                    transaction.copyFile(sourceFile, targetFile, digesters);
                } else {
                    result = false;
                }
            }
        }
    } catch (IOException e) {
        throw new ArtifactConversionException(Messages.getString("error.copying.artifact"), e); //$NON-NLS-1$
    }
    return result;
}