@Override public DependencyMetadata resolveDependencyMetadata( Dependency query, final List<DependencyRepository> repositories) { try { if (Strings.isNullOrEmpty(query.getVersion())) { query = DependencyBuilder.create(query).setVersion("[,)"); } RepositorySystem system = container.lookup(RepositorySystem.class); MavenRepositorySystemSession session = setupRepoSession(system); DefaultArtifact artifact = new DefaultArtifact(query.toCoordinates()); ArtifactDescriptorRequest ar = new ArtifactDescriptorRequest(artifact, convertToMavenRepos(repositories), null); ArtifactDescriptorResult results = system.readArtifactDescriptor(session, ar); Artifact a = results.getArtifact(); Dependency d = DependencyBuilder.create() .setArtifactId(a.getArtifactId()) .setGroupId(a.getGroupId()) .setVersion(a.getVersion()); return new DependencyMetadataImpl(d, results); } catch (Exception e) { throw new ProjectModelException( "Unable to resolve any artifacts for query [" + query + "]", e); } }
@Override public void removePlugin(Dependency dependency) { MavenCoreFacet mavenCoreFacet = project.getFacet(MavenCoreFacet.class); List<Plugin> pomPlugins = mavenCoreFacet.getPOM().getBuild().getPlugins(); for (Plugin pomPlugin : pomPlugins) { if (pomPlugin.getGroupId().equals(dependency.getGroupId()) && pomPlugin.getArtifactId().equals(dependency.getArtifactId())) { Model pom = mavenCoreFacet.getPOM(); pom.getBuild().removePlugin(pomPlugin); mavenCoreFacet.setPOM(pom); } } }
@Override public boolean install() { DependencyFacet deps = project.getFacet(DependencyFacet.class); List<Dependency> versions = deps.resolveAvailableVersions("org.jboss.seam.forge:forge-shell-api:[,]"); Dependency version = shell.promptChoiceTyped("Install which version of the Forge API?", versions); deps.setProperty("forge.api.version", version.getVersion()); DependencyBuilder dep = DependencyBuilder.create("org.jboss.seam.forge:forge-shell-api:${forge.api.version}"); deps.addDependency(dep); return true; }
@Override public MavenPlugin getPlugin(Dependency dependency) { String groupId = dependency.getGroupId(); String artifactId = dependency.getArtifactId(); if (groupId == null || groupId.equals("")) { groupId = DEFAULT_GROUPID; } for (MavenPlugin mavenPlugin : listConfiguredPlugins()) { if (mavenPlugin.getDependency().getGroupId().equals(groupId) && mavenPlugin.getDependency().getArtifactId().equals(artifactId)) { return mavenPlugin; } } throw new PluginNotFoundException(groupId, artifactId); }
private VersionRangeResult getVersions( Dependency dep, final List<RemoteRepository> repositories) { try { String version = dep.getVersion(); if (Strings.isNullOrEmpty(version)) { dep = DependencyBuilder.create(dep).setVersion("[,)"); } else if (!version.matches("(\\(|\\[).*?(\\)|\\])")) { dep = DependencyBuilder.create(dep).setVersion("[" + version + "]"); } RepositorySystem maven = container.lookup(RepositorySystem.class); MavenRepositorySystemSession session = setupRepoSession(maven); VersionRangeRequest rangeRequest = new VersionRangeRequest(new DefaultArtifact(dep.toCoordinates()), repositories, null); VersionRangeResult rangeResult = maven.resolveVersionRange(session, rangeRequest); return rangeResult; } catch (Exception e) { throw new ProjectModelException("Failed to look up versions for [" + dep + "]", e); } }
@Override public List<DependencyResource> resolveDependencies( Dependency dep, final List<DependencyRepository> repositories) { List<DependencyResource> result = new ArrayList<DependencyResource>(); try { if (Strings.isNullOrEmpty(dep.getVersion())) { dep = DependencyBuilder.create(dep).setVersion("[,)"); } RepositorySystem system = container.lookup(RepositorySystem.class); MavenRepositorySystemSession session = setupRepoSession(system); DefaultArtifact artifact = new DefaultArtifact(dep.toCoordinates()); CollectRequest collectRequest = new CollectRequest( new org.sonatype.aether.graph.Dependency(artifact, null), convertToMavenRepos(repositories)); DependencyRequest request = new DependencyRequest(collectRequest, null); DependencyResult artifacts = system.resolveDependencies(session, request); for (ArtifactResult a : artifacts.getArtifactResults()) { File file = a.getArtifact().getFile(); Dependency d = DependencyBuilder.create() .setArtifactId(a.getArtifact().getArtifactId()) .setGroupId(a.getArtifact().getGroupId()) .setVersion(a.getArtifact().getVersion()); DependencyResource resource = new DependencyResource(factory, file, d); result.add(resource); } return result; } catch (Exception e) { throw new ProjectModelException("Unable to resolve an artifact", e); } }
@Command("set-parent") public void setParent( @Option( name = "parentId", description = "dependency identifier of parent, ex: \"org.jboss.seam.forge:forge-parent:1.0.0\"", required = false) final Dependency gav, @Option( name = "parentRelativePath", description = "relative location from the current project to the parent project root folder", type = PromptType.FILE_PATH, required = false) final String relativePath, @Option( name = "parentProjectRoot", description = "absolute location of a project to use as this project's direct parent", required = false) final Resource<?> path, final PipeOut out) { MavenCoreFacet mvn = project.getFacet(MavenCoreFacet.class); Parent parent = null; if (gav != null) { Assert.notNull( gav.getArtifactId(), "ArtifactId must not be null [" + gav.toCoordinates() + "]"); Assert.notNull(gav.getGroupId(), "GroupId must not be null [" + gav.toCoordinates() + "]"); Assert.notNull(gav.getVersion(), "Version must not be null [" + gav.toCoordinates() + "]"); parent = new Parent(); parent.setArtifactId(gav.getArtifactId()); parent.setGroupId(gav.getGroupId()); parent.setVersion(gav.getVersion()); if (relativePath != null) { parent.setRelativePath(relativePath); } Model pom = mvn.getPOM(); pom.setParent(parent); mvn.setPOM(pom); } else if ((path != null) && factory.containsProject(path.reify(DirectoryResource.class))) { Project parentProject = factory.findProject(path.reify(DirectoryResource.class)); MavenCoreFacet parentCore = parentProject.getFacet(MavenCoreFacet.class); parent = new Parent(); parent.setArtifactId(parentCore.getMavenProject().getArtifactId()); parent.setGroupId(parentCore.getMavenProject().getGroupId()); parent.setVersion(parentCore.getMavenProject().getVersion()); if (relativePath != null) { parent.setRelativePath(relativePath); } Model pom = mvn.getPOM(); pom.setParent(parent); mvn.setPOM(pom); } else if (relativePath != null) { PathspecParser parser = new PathspecParser(resources, shell.getCurrentProject().getProjectRoot(), relativePath); List<Resource<?>> resolvedResources = parser.resolve(); if (!resolvedResources.isEmpty() && factory.containsProject(resolvedResources.get(0).reify(DirectoryResource.class))) { Project parentProject = factory.findProject(resolvedResources.get(0).reify(DirectoryResource.class)); MavenCoreFacet parentCore = parentProject.getFacet(MavenCoreFacet.class); parent = new Parent(); parent.setArtifactId(parentCore.getMavenProject().getArtifactId()); parent.setGroupId(parentCore.getMavenProject().getGroupId()); parent.setVersion(parentCore.getMavenProject().getVersion()); parent.setRelativePath(relativePath); Model pom = mvn.getPOM(); pom.setParent(parent); mvn.setPOM(pom); } else { out.print(ShellColor.RED, "***ERROR***"); out.println(" relative path did not resolve to a Project [" + relativePath + "]"); } } else { out.print(ShellColor.RED, "***ERROR***"); out.println(" you must specify a path to or dependency id of the parent project."); } if (parent != null) { String parentId = parent.getGroupId() + ":" + parent.getArtifactId() + ":" + parent.getVersion() + " (" + (parent.getRelativePath() == null ? " " : parent.getRelativePath() + ")"); out.println("Set parent [ " + parentId + " ]"); } }