public void test005_snapshotAvailableFromLocalRepoAndWorkspace() throws Exception { IProject p1 = createExisting("t005-p3"); waitForJobsToComplete(); { IMavenProjectFacade f = manager.create(p1, monitor); Set<Artifact> artifacts = getMavenProjectArtifacts(f); assertEquals(1, artifacts.size()); Artifact a = artifacts.iterator().next(); assertEquals(true, a.isResolved()); // assertTrue(a.getFile().getAbsolutePath().startsWith(repo.getAbsolutePath())); assertStartWith(repo.getAbsolutePath(), a.getFile().getAbsolutePath()); } IProject p2 = createExisting("t005-p4"); waitForJobsToComplete(); { IMavenProjectFacade f = manager.create(p1, monitor); Set<Artifact> artifacts = getMavenProjectArtifacts(f); assertEquals(1, artifacts.size()); Artifact a = artifacts.iterator().next(); assertEquals(true, a.isResolved()); assertEquals(p2.getFile(IMavenConstants.POM_FILE_NAME).getLocation().toFile(), a.getFile()); } }
/** * Resolve source artifacts and download them if <code>downloadSources</code> is <code>true</code> * . Source and javadocs artifacts will be attached to the <code>IdeDependency</code> * * @param deps resolved dependencies */ private void resolveSourceArtifacts(IdeDependency[] deps) { ArtifactRepository localRepository = getLocalRepository(); ArtifactResolver artifactResolver = getArtifactResolver(); ArtifactFactory artifactFactory = getArtifactFactory(); // if downloadSources is off, just check local repository for reporting missing jars List remoteRepos = getDownloadSources() ? getRemoteArtifactRepositories() : Collections.EMPTY_LIST; for (int j = 0; j < deps.length; j++) { IdeDependency dependency = deps[j]; if (dependency.isReferencedProject() || dependency.isSystemScoped()) { // source artifact not needed continue; } // source artifact: use the "sources" classifier added by the source plugin Artifact sourceArtifact = IdeUtils.resolveArtifactWithClassifier( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), "sources", localRepository, artifactResolver, //$NON-NLS-1$ artifactFactory, remoteRepos, getLog()); if (sourceArtifact.isResolved()) { dependency.setSourceAttachment(sourceArtifact.getFile()); } else { // try using a plain javadoc jar if the source jar is not available Artifact javadocArtifact = IdeUtils.resolveArtifactWithClassifier( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), "javadoc", localRepository, artifactResolver, //$NON-NLS-1$ artifactFactory, remoteRepos, getLog()); if (javadocArtifact.isResolved()) { dependency.setJavadocAttachment(javadocArtifact.getFile()); } // @todo also report deps without a source attachment but with a javadoc one? missingSourceDependencies.add(dependency); } } }
private void setTarget( TargetPlatformConfiguration result, MavenSession session, MavenProject project, Xpp3Dom configuration) { Xpp3Dom targetDom = configuration.getChild("target"); if (targetDom == null) { return; } Xpp3Dom artifactDom = targetDom.getChild("artifact"); if (artifactDom == null) { return; } Xpp3Dom groupIdDom = artifactDom.getChild("groupId"); Xpp3Dom artifactIdDom = artifactDom.getChild("artifactId"); Xpp3Dom versionDom = artifactDom.getChild("version"); if (groupIdDom == null || artifactIdDom == null || versionDom == null) { return; } Xpp3Dom classifierDom = artifactDom.getChild("classifier"); String groupId = groupIdDom.getValue(); String artifactId = artifactIdDom.getValue(); String version = versionDom.getValue(); String classifier = classifierDom != null ? classifierDom.getValue() : null; File targetFile = null; for (MavenProject otherProject : session.getProjects()) { if (groupId.equals(otherProject.getGroupId()) && artifactId.equals(otherProject.getArtifactId()) && version.equals(otherProject.getVersion())) { targetFile = new File(otherProject.getBasedir(), classifier + ".target"); break; } } if (targetFile == null) { Artifact artifact = repositorySystem.createArtifactWithClassifier( groupId, artifactId, version, "target", classifier); ArtifactResolutionRequest request = new ArtifactResolutionRequest(); request.setArtifact(artifact); request.setLocalRepository(session.getLocalRepository()); request.setRemoteRepositories(project.getRemoteArtifactRepositories()); repositorySystem.resolve(request); if (!artifact.isResolved()) { throw new RuntimeException( "Could not resolve target platform specification artifact " + artifact); } targetFile = artifact.getFile(); } result.setTarget(targetFile); }
protected Artifact resolve(Artifact artifact) throws MojoExecutionException { if (!artifact.isResolved()) { try { resolver.resolve(artifact, remoteRepositories, localRepository); } catch (AbstractArtifactResolutionException e) { throw new MojoExecutionException("Unable to resolve artifact: " + artifact, e); } } return artifact; }
public void test017_moduleRefresh() throws Exception { IProject p2 = createExisting("t017-p2"); IProject p3 = createExisting("t017-p3"); IProject p4 = createExisting("t017-p4"); waitForJobsToComplete(); IFile pom = p3.getFile("pom.xml"); MavenProject mavenProject = manager.create(pom, false, null).getMavenProject(monitor); Artifact a = mavenProject.getArtifacts().iterator().next(); assertTrue(a.isResolved()); assertEquals( p4.getFile(IMavenConstants.POM_FILE_NAME).getLocation().toFile().getAbsoluteFile(), a.getFile().getAbsoluteFile()); copyContent(p2, "pom_changed.xml", "pom.xml"); mavenProject = manager.create(pom, false, null).getMavenProject(monitor); a = mavenProject.getArtifacts().iterator().next(); assertFalse(a.isResolved()); }
public void testWorkspaceResolutionApi() throws Exception { IProject[] projects = importProjects( "projects/simple-pom", new String[] {"pom.xml"}, new ResolverConfiguration()); assertEquals(1, projects.length); assertNotNull(projects[0]); Artifact artifact = new DefaultArtifact( "org.eclipse.m2e.projects", "simple-pom", "1.0.0", null, "pom", "", null); artifact = manager.getWorkspaceLocalRepository().find(artifact); assertTrue(artifact.isResolved()); assertNotNull(artifact.getFile()); assertEquals(projects[0].getFile("pom.xml").getLocation().toFile(), artifact.getFile()); }
public void test001_dependencyOnModuleWithInheritedGroupAndVersion() throws Exception { IProject p1 = createExisting("t001-p1"); IProject p2 = createExisting("t001-p2"); IProject p3 = createExisting("t001-p3"); waitForJobsToComplete(); IMavenProjectFacade f1 = manager.create(p1, monitor); Set<Artifact> artifacts = getMavenProjectArtifacts(f1); assertEquals(1, artifacts.size()); Artifact a1 = artifacts.iterator().next(); assertEquals(true, a1.isResolved()); assertEquals(p2.getFile(IMavenConstants.POM_FILE_NAME).getLocation().toFile(), a1.getFile()); IMavenProjectFacade f2 = manager.create(p2, monitor); assertNotNull(f2); IMavenProjectFacade f3 = manager.create(p3, monitor); assertNotNull(f3); }
/** * Filters the specified {@link Artifact}s to those that match the following criteria: * * <ul> * <li>{@link Artifact#isResolved()} is <code>true</code> * <li>{@link Artifact#getArtifactHandler().getExtension()} equals "jar" * <li>{@link Artifact#getScope()} equals {@link Artifact#SCOPE_COMPILE} * <li>{@link Artifact#getFile()} returns a {@link File} where {@link File#isFile()} is <code> * true</code> * </ul> * * @param artifacts the {@link Set} of {@link Artifact}s to filter * @return the actual JAR {@link File}s available from the specified {@link Artifact}s */ public static List<File> filterToResolvedJars(List<Artifact> artifacts) { List<File> resolvedJarArtifacts = new ArrayList<File>(); ScopeArtifactFilter filter = new ScopeArtifactFilter(Artifact.SCOPE_COMPILE_PLUS_RUNTIME); for (Artifact artifact : artifacts) { // Ensure that this Artifact should be included if (!artifact.isResolved()) continue; if (artifact.getArtifactHandler() == null || !"jar".equalsIgnoreCase(artifact.getArtifactHandler().getExtension())) continue; if (!filter.include(artifact)) continue; // Ensure that the Artifact resolves to a File that we can use File artifactJarFile = artifact.getFile(); if (!artifactJarFile.isFile()) continue; resolvedJarArtifacts.add(artifactJarFile); } return resolvedJarArtifacts; }
protected List<String> getValuesToMatch(Artifact artifact) { List<String> valuesToMatch = new ArrayList<String>(); File file = artifact.getFile(); if (file == null) { if (artifact.isResolved()) { MavenClientFactory.getLog().warn("Artifact " + artifact + " doesn't contain a file"); } else if (!Artifact.SCOPE_PROVIDED.equals(artifact.getScope())) { // ignore provided artifacts; raise a warning for non provided MavenClientFactory.getLog().warn("Artifact " + artifact + " unresolved"); } return valuesToMatch; } // ignore non jar files if (!file.getName().endsWith(".jar")) { return valuesToMatch; } try { JarFile jarFile = new JarFile(file, true); Manifest mf = jarFile.getManifest(); if (mf != null) { Attributes attributes = mf.getMainAttributes(); if (attributes != null) { String bundleCategories = attributes.getValue(MANIFEST_BUNDLE_CATEGORY); if (bundleCategories != null) { StringTokenizer st = new StringTokenizer(bundleCategories, MANIFEST_BUNDLE_CATEGORY_TOKEN); while (st.hasMoreTokens()) { valuesToMatch.add(st.nextToken()); } } } } else { MavenClientFactory.getLog().warn("Artifact " + artifact + " doesn't contain a manifest"); } } catch (IOException e) { MavenClientFactory.getLog() .error("error while inspecting this jar manifest: " + artifact.getFile(), e); } return valuesToMatch; }
private void addMissingArtifactProblemInfos( MavenProject mavenProject, SourceLocation location, List<MavenProblemInfo> knownProblems) { Set<Artifact> artifacts = mavenProject.getArtifacts(); all_artifacts_loop: for (Artifact mavenArtifact : artifacts) { if (!mavenArtifact.isResolved()) { org.eclipse.aether.artifact.Artifact artifact = RepositoryUtils.toArtifact(mavenArtifact); for (MavenProblemInfo problem : knownProblems) { if (problem instanceof ArtifactNotFoundProblemInfo) { ArtifactNotFoundProblemInfo artifactNotFoundProblemInfo = (ArtifactNotFoundProblemInfo) problem; if (equals(artifactNotFoundProblemInfo.getArtifact(), artifact)) { continue all_artifacts_loop; } } } knownProblems.add( new ArtifactNotFoundProblemInfo(artifact, mavenConfiguration.isOffline(), location)); } } }
private boolean isLib(Artifact artifact) { return artifact.isResolved() && SCOPES.contains(artifact.getScope()) && "jar".equals(artifact.getType()) && projectReferences.get(getProjectReferenceId(artifact)) == null; }
private void resolveOld( Artifact artifact, List<ArtifactRepository> remoteRepositories, RepositorySystemSession session) throws ArtifactResolutionException, ArtifactNotFoundException { if (artifact == null) { return; } if (Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) { File systemFile = artifact.getFile(); if (systemFile == null) { throw new ArtifactNotFoundException( "System artifact: " + artifact + " has no file attached", artifact); } if (!systemFile.exists()) { throw new ArtifactNotFoundException( "System artifact: " + artifact + " not found in path: " + systemFile, artifact); } if (!systemFile.isFile()) { throw new ArtifactNotFoundException( "System artifact: " + artifact + " is not a file: " + systemFile, artifact); } artifact.setResolved(true); return; } if (!artifact.isResolved()) { ArtifactResult result; try { ArtifactRequest artifactRequest = new ArtifactRequest(); artifactRequest.setArtifact(RepositoryUtils.toArtifact(artifact)); artifactRequest.setRepositories(RepositoryUtils.toRepos(remoteRepositories)); // Maven 2.x quirk: an artifact always points at the local repo, regardless whether resolved // or not LocalRepositoryManager lrm = session.getLocalRepositoryManager(); String path = lrm.getPathForLocalArtifact(artifactRequest.getArtifact()); artifact.setFile(new File(lrm.getRepository().getBasedir(), path)); result = repoSystem.resolveArtifact(session, artifactRequest); } catch (org.eclipse.aether.resolution.ArtifactResolutionException e) { if (e.getCause() instanceof org.eclipse.aether.transfer.ArtifactNotFoundException) { throw new ArtifactNotFoundException( e.getMessage(), artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(), artifact.getClassifier(), remoteRepositories, artifact.getDownloadUrl(), artifact.getDependencyTrail(), e); } else { throw new ArtifactResolutionException(e.getMessage(), artifact, remoteRepositories, e); } } artifact.selectVersion(result.getArtifact().getVersion()); artifact.setFile(result.getArtifact().getFile()); artifact.setResolved(true); if (artifact.isSnapshot()) { Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher(artifact.getVersion()); if (matcher.matches()) { Snapshot snapshot = new Snapshot(); snapshot.setTimestamp(matcher.group(2)); try { snapshot.setBuildNumber(Integer.parseInt(matcher.group(3))); artifact.addMetadata(new SnapshotArtifactRepositoryMetadata(artifact, snapshot)); } catch (NumberFormatException e) { logger.warn( "Invalid artifact version " + artifact.getVersion() + ": " + e.getMessage()); } } } } }