Author: tchemit Date: 2010-06-19 02:41:12 +0200 (Sat, 19 Jun 2010) New Revision: 721 Url: http://nuiton.org/repositories/revision/maven-helper-plugin/721 Log: improve extraRepositories use a Map + skip snapshot dependencies Modified: trunk/src/main/java/org/nuiton/helper/plugin/CheckCentralSafePlugin.java Modified: trunk/src/main/java/org/nuiton/helper/plugin/CheckCentralSafePlugin.java =================================================================== --- trunk/src/main/java/org/nuiton/helper/plugin/CheckCentralSafePlugin.java 2010-06-18 23:52:29 UTC (rev 720) +++ trunk/src/main/java/org/nuiton/helper/plugin/CheckCentralSafePlugin.java 2010-06-19 00:41:12 UTC (rev 721) @@ -31,15 +31,10 @@ import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.manager.WagonConfigurationException; import org.apache.maven.artifact.manager.WagonManager; -import org.apache.maven.artifact.metadata.ArtifactMetadata; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout; -import org.apache.maven.artifact.repository.metadata.Metadata; -import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata; -import org.apache.maven.artifact.resolver.ArtifactNotFoundException; -import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.artifact.resolver.ArtifactResolver; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.logging.Log; @@ -61,6 +56,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Set; /** @@ -167,12 +163,12 @@ protected List<ArtifactRepository> safeRepositories; /** - * Extra Remote Repository urls to authorized separated by comma. + * Map of extra remote Repositories. Keys are repository id and Values are repository url. * - * @parameter expression="${extraRepositories}" + * @parameter * @since 1.0.0 */ - protected String extraRepositories; + protected Map<String, String> extraRepositories; /** * Dependencies of the project. @@ -231,33 +227,35 @@ ArtifactRepositoryLayout repositoryLayout = new DefaultRepositoryLayout(); - ArtifactRepository remoteRepo; + List<String> ids = new ArrayList<String>(extraRepositories.keySet()); + ids.add(0, "central"); + extraRepositories.put("central", "http://repo1.maven.org/maven2/"); - remoteRepo = artifactRepositoryFactory.createDeploymentArtifactRepository( - "central", - "http://repo1.maven.org/maven2/", - repositoryLayout, false); + for (String id : ids) { + String url = extraRepositories.get(id); + url = url.trim(); - log.info("Will use Repository " + remoteRepo.getUrl()); - safeRepositories.add(remoteRepo); + ArtifactRepository remoteRepo = artifactRepositoryFactory.createDeploymentArtifactRepository( + String.valueOf(id), + url, + repositoryLayout, true); - if (!StringUtils.isEmpty(extraRepositories)) { - for (String url : extraRepositories.split(",")) { - url = url.trim(); - - remoteRepo = artifactRepositoryFactory.createDeploymentArtifactRepository( - url, - url, - repositoryLayout, false); - - log.info("Will use Repository " + remoteRepo.getUrl()); - safeRepositories.add(remoteRepo); - } + log.info("Will use Repository " + remoteRepo.getUrl()); + safeRepositories.add(remoteRepo); } + // resolve also plugins + artifacts.addAll(project.getPluginArtifacts()); + for (Iterator<?> iterator = artifacts.iterator(); iterator.hasNext();) { Artifact artifact = (Artifact) iterator.next(); + if (artifact.isSnapshot()) { + + log.warn("Remove snapshot dependency : " + artifact); + iterator.remove(); + } + if (project.getArtifactId().equals(artifact.getArtifactId()) && project.getGroupId().equals(artifact.getGroupId()) && project.getVersion().equals(artifact.getVersion())) { @@ -372,14 +370,20 @@ } protected boolean dependencyExistsInRepo(Wagon wagon, - ArtifactRepository repo, - Artifact artifact) { + ArtifactRepository repo, + Artifact artifact) { Log log = getLog(); + if (!artifact.isSnapshot()) { + } try { - return wagon.resourceExists(StringUtils.replace(getDependencyUrlFromRepository(artifact, repo), repo.getUrl(), "")); + Artifact copyArtifact = ArtifactUtils.copyArtifact(artifact); + + String path = repo.getUrl() + "/" + repo.pathOf(copyArtifact); + + return wagon.resourceExists(StringUtils.replace(path, repo.getUrl(), "")); } catch (TransferFailedException e) { if (log.isDebugEnabled()) { log.error("Unable to determine if resource " + artifact + " exists in " + repo.getUrl(), e); @@ -445,63 +449,6 @@ } /** - * @param artifact not null - * @param repo not null - * @return the artifact url in the given repo for the given artifact. If it is a snapshot artifact, the version - * will be the timestamp and the build number from the metadata. Could return null if the repo is blacklisted. - */ - public String getDependencyUrlFromRepository(Artifact artifact, ArtifactRepository repo) { - if (repo.isBlacklisted()) { - return null; - } - - Log log = getLog(); - Artifact copyArtifact = ArtifactUtils.copyArtifact(artifact); - // Try to get the last artifact repo name depending the snapshot version - if (!artifact.isSnapshot() || !repo.getSnapshots().isEnabled()) { - return repo.getUrl() + "/" + repo.pathOf(copyArtifact); - } - - if (artifact.getBaseVersion().equals(artifact.getVersion())) { - // Try to resolve it if not already done - if (artifact.getMetadataList() == null || artifact.getMetadataList().isEmpty()) { - try { - resolver.resolve(artifact, safeRepositories, localRepository); - } catch (ArtifactResolutionException e) { - log.error("Artifact: " + artifact.getId() + " could not be resolved."); - } catch (ArtifactNotFoundException e) { - log.error("Artifact: " + artifact.getId() + " was not found."); - } - } - - for (Object o : artifact.getMetadataList()) { - - ArtifactMetadata m = (ArtifactMetadata) o; - - if (m instanceof SnapshotArtifactRepositoryMetadata) { - SnapshotArtifactRepositoryMetadata snapshotMetadata = (SnapshotArtifactRepositoryMetadata) m; - - Metadata metadata = snapshotMetadata.getMetadata(); - if (metadata.getVersioning() == null || metadata.getVersioning().getSnapshot() == null - || metadata.getVersioning().getSnapshot().isLocalCopy() - || metadata.getVersioning().getSnapshot().getTimestamp() == null) { - continue; - } - - // create the version according SnapshotTransformation - String version = - StringUtils.replace(copyArtifact.getVersion(), Artifact.SNAPSHOT_VERSION, - metadata.getVersioning().getSnapshot().getTimestamp()) - + "-" + metadata.getVersioning().getSnapshot().getBuildNumber(); - copyArtifact.setVersion(version); - } - } - } - - return repo.getUrl() + "/" + repo.pathOf(copyArtifact); - } - - /** * Convenience method to map a <code>Proxy</code> object from the user system settings to a <code>ProxyInfo</code> * object. *