List of usage examples for org.apache.maven.artifact Artifact getRepository
ArtifactRepository getRepository();
From source file:br.com.anteros.restdoc.maven.plugin.util.ResourceResolver.java
License:Apache License
private static Artifact createResourceArtifact(final Artifact artifact, final String classifier, final SourceResolverConfig config) { final DefaultArtifact a = (DefaultArtifact) config.artifactFactory().createArtifactWithClassifier( artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), "jar", classifier); a.setRepository(artifact.getRepository()); return a;//w w w . j a v a 2 s . co m }
From source file:hudson.maven.artifact.transform.AbstractVersionTransformation.java
License:Apache License
protected String resolveVersion(Artifact artifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories) throws RepositoryMetadataResolutionException { RepositoryMetadata metadata;/* w ww. j a v a2 s . c o m*/ // Don't use snapshot metadata for LATEST (which isSnapshot returns true for) if (!artifact.isSnapshot() || Artifact.LATEST_VERSION.equals(artifact.getBaseVersion())) { metadata = new ArtifactRepositoryMetadata(artifact); } else { metadata = new SnapshotArtifactRepositoryMetadata(artifact); } repositoryMetadataManager.resolve(metadata, remoteRepositories, localRepository); artifact.addMetadata(metadata); Metadata repoMetadata = metadata.getMetadata(); String version = null; if (repoMetadata != null && repoMetadata.getVersioning() != null) { version = constructVersion(repoMetadata.getVersioning(), artifact.getBaseVersion()); } if (version == null) { // use the local copy, or if it doesn't exist - go to the remote repo for it version = artifact.getBaseVersion(); } // TODO: also do this logging for other metadata? // TODO: figure out way to avoid duplicated message if (getLogger().isDebugEnabled()) { if (!version.equals(artifact.getBaseVersion())) { String message = artifact.getArtifactId() + ": resolved to version " + version; if (artifact.getRepository() != null) { message += " from repository " + artifact.getRepository().getId(); } else { message += " from local repository"; } getLogger().debug(message); } else { // Locally installed file is newer, don't use the resolved version getLogger().debug(artifact.getArtifactId() + ": using locally installed snapshot"); } } return version; }
From source file:net.eckenfels.mavenplugins.lockdeps.LockDependenciesMojo.java
License:Apache License
public void execute() throws MojoExecutionException { Log log = getLog();/*from w w w . j av a 2s . co m*/ log.debug("Now scanning dependencies..." + project); Set<Artifact> deps = project.getArtifacts(); for (Artifact a : deps) { ArtifactRepository rep = a.getRepository(); log.info("artifact: " + a.getId() + " (" + a.getScope() + ") from " + a.getDownloadUrl() + " " + a.getFile() + " repid=" + ((rep != null) ? rep.getId() : null)); } deps = project.getPluginArtifacts(); for (Artifact a : deps) { log.info("plugin artifact: " + a.getId() + " (" + a.getScope() + ") from " + a.getDownloadUrl() + " " + a.getFile()); } }
From source file:org.apache.archiva.converter.artifact.LegacyToDefaultConverter.java
License:Apache License
@Override public void convert(Artifact artifact, ArtifactRepository targetRepository) throws ArtifactConversionException { if (artifact.getRepository().getUrl().equals(targetRepository.getUrl())) { throw new ArtifactConversionException(Messages.getString("exception.repositories.match")); //$NON-NLS-1$ }/*from w w w. j a va 2 s . c o m*/ if (!validateMetadata(artifact)) { addWarning(artifact, Messages.getString("unable.to.validate.metadata")); //$NON-NLS-1$ return; } FileTransaction transaction = new FileTransaction(); if (!copyPom(artifact, targetRepository, transaction)) { addWarning(artifact, Messages.getString("unable.to.copy.pom")); //$NON-NLS-1$ return; } if (!copyArtifact(artifact, targetRepository, transaction)) { addWarning(artifact, Messages.getString("unable.to.copy.artifact")); //$NON-NLS-1$ return; } Metadata metadata = createBaseMetadata(artifact); Versioning versioning = new Versioning(); versioning.addVersion(artifact.getBaseVersion()); metadata.setVersioning(versioning); updateMetadata(new ArtifactRepositoryMetadata(artifact), targetRepository, metadata, transaction); metadata = createBaseMetadata(artifact); metadata.setVersion(artifact.getBaseVersion()); versioning = new Versioning(); Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher(artifact.getVersion()); if (matcher.matches()) { Snapshot snapshot = new Snapshot(); snapshot.setBuildNumber(Integer.parseInt(matcher.group(3))); snapshot.setTimestamp(matcher.group(2)); versioning.setSnapshot(snapshot); } // TODO: merge latest/release/snapshot from source instead metadata.setVersioning(versioning); updateMetadata(new SnapshotArtifactRepositoryMetadata(artifact), targetRepository, metadata, transaction); if (!dryrun) { try { transaction.commit(); } catch (TransactionException e) { throw new ArtifactConversionException(Messages.getString("transaction.failure", e.getMessage()), e); //$NON-NLS-1$ } } }
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 w w w . j a v a 2s . co 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 validateMetadata(Artifact artifact) throws ArtifactConversionException { ArtifactRepository repository = artifact.getRepository(); boolean result = true; RepositoryMetadata repositoryMetadata = new ArtifactRepositoryMetadata(artifact); File file = new File(repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata(repositoryMetadata)); if (file.exists()) { Metadata metadata = readMetadata(file); result = validateMetadata(metadata, repositoryMetadata, artifact); }//from w w w. j av a 2s . com repositoryMetadata = new SnapshotArtifactRepositoryMetadata(artifact); file = new File(repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata(repositoryMetadata)); if (file.exists()) { Metadata metadata = readMetadata(file); result = result && validateMetadata(metadata, repositoryMetadata, artifact); } return result; }
From source file:org.apache.karaf.tooling.features.AbstractFeatureMojo.java
License:Apache License
/** * Resolves and copies the given artifact to the repository path. * Prefers to resolve using the repository of the artifact if present. * /*from w ww.jav a 2s . co m*/ * @param artifact * @param remoteRepos */ @SuppressWarnings("deprecation") protected void resolveArtifact(Artifact artifact, List<ArtifactRepository> remoteRepos) { try { if (artifact == null) { return; } List<ArtifactRepository> usedRemoteRepos = artifact.getRepository() != null ? Collections.singletonList(artifact.getRepository()) : remoteRepos; artifactResolver.resolve(artifact, usedRemoteRepos, localRepo); } catch (Exception e) { if (failOnArtifactResolutionError) { throw new RuntimeException("Can't resolve artifact " + artifact, e); } getLog().warn("Can't resolve artifact " + artifact); getLog().debug(e); } }
From source file:org.codehaus.mojo.natives.plugin.NativeLinkMojo.java
License:Open Source License
/** * /* ww w. ja v a2 s.c om*/ */ private void attachPrimaryArtifact() { Artifact artifact = this.project.getArtifact(); if (null == this.classifier) { artifact.setFile(new File(this.linkerOutputDirectory + "/" + this.project.getBuild().getFinalName() + "." + this.project.getArtifact().getArtifactHandler().getExtension())); } else { //install primary artifact as a classifier DefaultArtifact clone = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersionRange().cloneOf(), artifact.getScope(), artifact.getType(), classifier, artifact.getArtifactHandler(), artifact.isOptional()); clone.setRelease(artifact.isRelease()); clone.setResolvedVersion(artifact.getVersion()); clone.setResolved(artifact.isResolved()); clone.setFile(artifact.getFile()); if (artifact.getAvailableVersions() != null) { clone.setAvailableVersions(new ArrayList(artifact.getAvailableVersions())); } clone.setBaseVersion(artifact.getBaseVersion()); clone.setDependencyFilter(artifact.getDependencyFilter()); if (artifact.getDependencyTrail() != null) { clone.setDependencyTrail(new ArrayList(artifact.getDependencyTrail())); } clone.setDownloadUrl(artifact.getDownloadUrl()); clone.setRepository(artifact.getRepository()); clone.setFile(new File(this.linkerOutputDirectory + "/" + this.project.getBuild().getFinalName() + "." + this.project.getArtifact().getArtifactHandler().getExtension())); project.setArtifact(clone); } }
From source file:org.codehaus.mojo.webstart.AbstractJnlpMojo.java
License:Apache License
private void processDependency(Artifact artifact) throws MojoExecutionException { // TODO: scope handler // Include runtime and compile time libraries if (!Artifact.SCOPE_SYSTEM.equals(artifact.getScope()) && !Artifact.SCOPE_PROVIDED.equals(artifact.getScope()) && !Artifact.SCOPE_TEST.equals(artifact.getScope())) { String type = artifact.getType(); if ("jar".equals(type) || "ejb-client".equals(type)) { // FIXME when signed, we should update the manifest. // see http://www.mail-archive.com/turbine-maven-dev@jakarta.apache.org/msg08081.html // and maven1: maven-plugins/jnlp/src/main/org/apache/maven/jnlp/UpdateManifest.java // or shouldn't we? See MOJO-7 comment end of October. final File toCopy = artifact.getFile(); if (toCopy == null) { getLog().error("artifact with no file: " + artifact); getLog().error("artifact download url: " + artifact.getDownloadUrl()); getLog().error("artifact repository: " + artifact.getRepository()); getLog().error("artifact repository: " + artifact.getVersion()); throw new IllegalStateException( "artifact " + artifact + " has no matching file, why? Check the logs..."); }/*from ww w. j a v a 2 s . c o m*/ String name = getDependencyFilenameStrategy().getDependencyFilename(artifact, outputJarVersions, isUseUniqueVersions()); boolean copied = copyJarAsUnprocessedToDirectoryIfNecessary(toCopy, getLibDirectory(), name); if (copied) { getModifiedJnlpArtifacts().add(name.substring(0, name.lastIndexOf('.'))); } packagedJnlpArtifacts.add(artifact); if (jnlp.isRequireMainClass()) { // try to find if this dependency contains the main class boolean containsMainClass = getArtifactUtil().artifactContainsClass(artifact, jnlp.getMainClass()); if (containsMainClass) { if (artifactWithMainClass == null) { artifactWithMainClass = artifact; getLog().debug("Found main jar. Artifact " + artifactWithMainClass + " contains the main class: " + jnlp.getMainClass()); } else { getLog().warn("artifact " + artifact + " also contains the main class: " + jnlp.getMainClass() + ". IGNORED."); } } } } else // FIXME how do we deal with native libs? // we should probably identify them and package inside jars that we timestamp like the native lib // to avoid repackaging every time. What are the types of the native libs? { verboseLog("Skipping artifact of type " + type + " for " + getLibDirectory().getName()); } // END COPY } else { verboseLog("Skipping artifact of scope " + artifact.getScope() + " for " + getLibDirectory().getName()); } }
From source file:org.codehaus.mojo.webstart.AbstractJnlpMojo.java
License:Apache License
private void processExtensionDependency(JnlpExtension extension, Artifact artifact) throws MojoExecutionException { // TODO: scope handler // Include runtime and compile time libraries if (!Artifact.SCOPE_SYSTEM.equals(artifact.getScope()) && !Artifact.SCOPE_PROVIDED.equals(artifact.getScope()) && !Artifact.SCOPE_TEST.equals(artifact.getScope())) { String type = artifact.getType(); if ("jar".equals(type) || "ejb-client".equals(type)) { // FIXME when signed, we should update the manifest. // see http://www.mail-archive.com/turbine-maven-dev@jakarta.apache.org/msg08081.html // and maven1: maven-plugins/jnlp/src/main/org/apache/maven/jnlp/UpdateManifest.java // or shouldn't we? See MOJO-7 comment end of October. final File toCopy = artifact.getFile(); if (toCopy == null) { getLog().error("artifact with no file: " + artifact); getLog().error("artifact download url: " + artifact.getDownloadUrl()); getLog().error("artifact repository: " + artifact.getRepository()); getLog().error("artifact repository: " + artifact.getVersion()); throw new IllegalStateException( "artifact " + artifact + " has no matching file, why? Check the logs..."); }//from w w w.j a v a 2 s .c om // check jar is signed boolean jarSigned = isJarSigned(toCopy); if (!jarSigned) { throw new IllegalStateException( "artifact " + artifact + " must be signed as part of an extension.."); } String targetFilename = getDependencyFilenameStrategy().getDependencyFilename(artifact, outputJarVersions, isUseUniqueVersions()); File targetFile = new File(getLibDirectory(), targetFilename); boolean copied = getIoUtil().shouldCopyFile(toCopy, targetFile); if (copied) { getIoUtil().copyFile(toCopy, targetFile); verboseLog("copy extension artifact " + toCopy); } else { verboseLog("already up to date artifact " + toCopy); } // save the artifact dependency for the extension List<Artifact> deps = extensionsJnlpArtifacts.get(extension); if (deps == null) { deps = new ArrayList<Artifact>(); extensionsJnlpArtifacts.put(extension, deps); } deps.add(artifact); } else // FIXME how do we deal with native libs? // we should probably identify them and package inside jars that we timestamp like the native lib // to avoid repackaging every time. What are the types of the native libs? { verboseLog("Skipping artifact of type " + type + " for " + getLibDirectory().getName()); } // END COPY } else { verboseLog("Skipping artifact of scope " + artifact.getScope() + " for " + getLibDirectory().getName()); } }