Java Code Examples for org.apache.maven.model.DependencyManagement#getDependencies()

The following examples show how to use org.apache.maven.model.DependencyManagement#getDependencies() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Bom.java    From cloud-opensource-java with Apache License 2.0 6 votes vote down vote up
public static Bom readBom(Path pomFile) throws MavenRepositoryException {
  RepositorySystem system = RepositoryUtility.newRepositorySystem();
  RepositorySystemSession session = RepositoryUtility.newSession(system);

  MavenProject mavenProject = RepositoryUtility.createMavenProject(pomFile, session);
  String coordinates = mavenProject.getGroupId() + ":" + mavenProject.getArtifactId() 
      + ":" + mavenProject.getVersion();
  DependencyManagement dependencyManagement = mavenProject.getDependencyManagement();
  List<org.apache.maven.model.Dependency> dependencies = dependencyManagement.getDependencies();

  ArtifactTypeRegistry registry = session.getArtifactTypeRegistry();
  ImmutableList<Artifact> artifacts = dependencies.stream()
      .map(dependency -> RepositoryUtils.toDependency(dependency, registry))
      .map(Dependency::getArtifact)
      .filter(artifact -> !shouldSkipBomMember(artifact))
      .collect(ImmutableList.toImmutableList());
  
  Bom bom = new Bom(coordinates, artifacts);
  return bom;
}
 
Example 2
Source File: ConfluenceDeployMojo.java    From maven-confluence-plugin with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param projectId
 * @param dependencyManagement
 * @return
 * @throws ProjectBuildingException
 */
private Map<String,Artifact> createManagedVersionMap(String projectId, DependencyManagement dependencyManagement) throws ProjectBuildingException {
    Map<String,Artifact> map;
    if (dependencyManagement != null && dependencyManagement.getDependencies() != null) {
        map = new HashMap<>();
        for (Dependency d : dependencyManagement.getDependencies()) {
            try {
                VersionRange versionRange = VersionRange.createFromVersionSpec(d.getVersion());
                Artifact artifact = factory.createDependencyArtifact(d.getGroupId(), d.getArtifactId(),
                        versionRange, d.getType(), d.getClassifier(),
                        d.getScope());
                map.put(d.getManagementKey(), artifact);
            } catch (InvalidVersionSpecificationException e) {
                throw new ProjectBuildingException(projectId, "Unable to parse version '" + d.getVersion()
                        + "' for dependency '" + d.getManagementKey() + "': " + e.getMessage(), e);
            }
        }
    } else {
        map = Collections.emptyMap();
    }
    return map;
}
 
Example 3
Source File: MavenUtil.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
public static String lookupVersion(final MavenProject project, final MavenDependency mavenDependency) throws MojoExecutionException {
  final DependencyManagement dependencyManagement = project.getModel().getDependencyManagement();
  if (dependencyManagement != null)
    for (final Dependency dependency : dependencyManagement.getDependencies())
      if (dependency.getGroupId().equals(mavenDependency.getGroupId()) && dependency.getArtifactId().equals(mavenDependency.getArtifactId()))
        return dependency.getVersion();

  if (project.getParent() == null)
    throw new MojoExecutionException("Was not able to find the version of: " + mavenDependency.getGroupId() + ":" + mavenDependency.getArtifactId());

  return lookupVersion(project.getParent(), mavenDependency);
}
 
Example 4
Source File: PomCopyManagedDependencies.java    From butterfly with MIT License 5 votes vote down vote up
@Override
List<Dependency> getMavenDependencies(Model mavenModel) {
    DependencyManagement dependencyManagement = mavenModel.getDependencyManagement();
    if (dependencyManagement == null) {
        return Collections.emptyList();
    }
    return dependencyManagement.getDependencies();
}
 
Example 5
Source File: DependencyNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public boolean isManaged() {
    DependencyManagement dm = project.getLookup().lookup(NbMavenProject.class).getMavenProject().getDependencyManagement();
    if (dm != null) {
        List<Dependency> dmList = dm.getDependencies();
        for (Dependency d : dmList) {
            if (art.getGroupId().equals(d.getGroupId())
                    && art.getArtifactId().equals(d.getArtifactId())) {
                return true;
            }
        }
    }
    return false;
}
 
Example 6
Source File: AddDependencyPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static List<Dependency> getDependenciesFromDM(MavenProject project, Project nbprj) {
    NbMavenProjectImpl p = nbprj.getLookup().lookup(NbMavenProjectImpl.class);
    MavenProject localProj = project;
    DependencyManagement curDM;
    List<Dependency> result = new ArrayList<Dependency>();
    //mkleint: without the managementKey checks I got some entries multiple times.
    // do we actually need to traverse the parent poms, are they completely resolved anyway?
    //XXX
    Set<String> knownKeys = new HashSet<String>();

    while (localProj != null) {
        curDM = localProj.getDependencyManagement();
        if (curDM != null) {
            @SuppressWarnings("unchecked")
            List<Dependency> ds = curDM.getDependencies();
            for (Dependency d : ds) {
                if (knownKeys.contains(d.getManagementKey())) {
                    continue;
                }
                result.add(d);
                knownKeys.add(d.getManagementKey());
            }
        }
        try {
            localProj = p.loadParentOf(EmbedderFactory.getProjectEmbedder(), localProj);
        } catch (ProjectBuildingException x) {
            break;
        }
    }
    Collections.sort(result, new Comparator<Dependency>() {

        @Override
        public int compare(Dependency o1, Dependency o2) {
            return o1.getManagementKey().compareTo(o2.getManagementKey());
        }
    });
    return result;
}
 
Example 7
Source File: LocationAwareMavenXpp3Writer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void writeDependencyManagement(DependencyManagement dependencyManagement, String tagName, XmlSerializer serializer)
        throws java.io.IOException {
    serializer.startTag(NAMESPACE, tagName);
    if ((dependencyManagement.getDependencies() != null) && (dependencyManagement.getDependencies().size() > 0)) {
        serializer.startTag(NAMESPACE, "dependencies");
        for (Iterator<Dependency> iter = dependencyManagement.getDependencies().iterator(); iter.hasNext();) {
            Dependency o = iter.next();
            writeDependency(o, "dependency", serializer);
        }
        serializer.endTag(NAMESPACE, "dependencies");
    }
    serializer.endTag(NAMESPACE, tagName);
}
 
Example 8
Source File: GraphConstructor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private int obtainManagedState(MavenDependencyNode dependencyNode) {        
    if (proj == null) {
        return GraphNode.UNMANAGED;
    }

    DependencyManagement dm = proj.getDependencyManagement();
    if (dm == null) {
        return GraphNode.UNMANAGED;
    }

    @SuppressWarnings("unchecked")
    List<Dependency> deps = dm.getDependencies();
    if (deps == null) {
        return GraphNode.UNMANAGED;
    }

    Artifact artifact = dependencyNode.getArtifact();
    String id = artifact.getArtifactId();
    String groupId = artifact.getGroupId();
    String version = artifact.getVersion();

    for (Dependency dep : deps) {
        if (id.equals(dep.getArtifactId()) && groupId.equals(dep.getGroupId())) {
            if (!version.equals(dep.getVersion())) {
                return GraphNode.OVERRIDES_MANAGED;
            } else {
                return GraphNode.MANAGED;
            }
        }
    }

    return GraphNode.UNMANAGED;
}
 
Example 9
Source File: AbstractVersionsStep.java    From unleash-maven-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private void setProjectReactorDependencyManagementVersion(MavenProject project, Document document) {
  DependencyManagement dependencyManagement = this.rawModels.getUnchecked(project).getDependencyManagement();
  if (dependencyManagement != null) {
    String dependenciesPath = "/dependencyManagement";
    List<Dependency> dependencies = dependencyManagement.getDependencies();
    for (Dependency dependency : dependencies) {
      trySetDependencyVersionFromReactorProjects(project, document, dependenciesPath, dependency);
    }
  }
}
 
Example 10
Source File: AbstractVersionsStep.java    From unleash-maven-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private void setProfilesReactorDependencyManagementVersion(MavenProject project, Document document) {
  List<Profile> profiles = this.rawModels.getUnchecked(project).getProfiles();
  for (Profile profile : profiles) {
    final String dependenciesPath = "/profiles/profile[id[text()='" + profile.getId() + "']]/dependencyManagement";
    DependencyManagement dependencyManagement = profile.getDependencyManagement();
    if (dependencyManagement != null) {
      List<Dependency> dependencies = dependencyManagement.getDependencies();
      for (Dependency dependency : dependencies) {
        trySetDependencyVersionFromReactorProjects(project, document, dependenciesPath, dependency);
      }
    }
  }
}
 
Example 11
Source File: GenerateBomMojo.java    From sundrio with Apache License 2.0 5 votes vote down vote up
/**
 * Returns all dependencies defined in dependency management of the root pom.
 *
 * @return
 */
private Set<Artifact> getProjectDependencyManagement() {
    Set<Artifact> result = new LinkedHashSet<Artifact>();
    DependencyManagement dependencyManagement = getProject().getDependencyManagement();
    if (dependencyManagement != null) {
        for (Dependency dependency : dependencyManagement.getDependencies()) {
            result.add(toArtifact(dependency));
        }
    }
    return result;
}
 
Example 12
Source File: Project.java    From pom-manipulation-ext with Apache License 2.0 5 votes vote down vote up
/**
 * This method will scan the dependencies in the dependencyManagement section of this project and return a
 * fully resolved list. Note that while updating the {@link Dependency} reference returned will be reflected
 * in the Model as it is the same object, if you wish to remove or add items to the Model then you must use {@link #getModel()}
 *
 * @param session MavenSessionHandler, used by {@link PropertyResolver}
 * @return a list of fully resolved {@link ArtifactRef} to the original {@link Dependency} (that were within DependencyManagement)
 * @throws ManipulationException if an error occurs
 */
public Map<ArtifactRef, Dependency> getResolvedManagedDependencies( MavenSessionHandler session ) throws ManipulationException
{
    Map<ArtifactRef, Dependency> resolvedManagedDependencies = new HashMap<>();

    final DependencyManagement dm = getModel().getDependencyManagement();
    if ( !( dm == null || dm.getDependencies() == null ) )
    {
        resolveDeps( session, dm.getDependencies(), false, resolvedManagedDependencies );
    }

    return resolvedManagedDependencies;
}
 
Example 13
Source File: ScalaCreateMavenProjectIT.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Test
public void testProjectGenerationFromScratchForScala() throws MavenInvocationException, IOException {

    testDir = initEmptyProject("projects/project-generation-scala");
    assertThat(testDir).isDirectory();
    invoker = initInvoker(testDir);

    Properties properties = new Properties();
    properties.put("projectGroupId", "org.acme");
    properties.put("projectArtifactId", "acme");
    properties.put("projectVersion", "1.0-SNAPSHOT");
    properties.put("extensions", "scala,resteasy-jsonb");
    setup(properties);

    // As the directory is not empty (log) navigate to the artifactID directory
    testDir = new File(testDir, "acme");

    assertThat(new File(testDir, "pom.xml")).isFile();
    assertThat(new File(testDir, "src/main/scala")).isDirectory();
    assertThat(new File(testDir, "src/main/resources/application.properties")).isFile();

    String config = Files
            .asCharSource(new File(testDir, "src/main/resources/application.properties"), Charsets.UTF_8)
            .read();
    assertThat(config).contains("key = value");

    assertThat(new File(testDir, "src/main/docker/Dockerfile.native")).isFile();
    assertThat(new File(testDir, "src/main/docker/Dockerfile.jvm")).isFile();

    Model model = loadPom(testDir);
    final DependencyManagement dependencyManagement = model.getDependencyManagement();
    final List<Dependency> dependencies = dependencyManagement.getDependencies();
    assertThat(dependencies.stream()
            .anyMatch(d -> d.getArtifactId().equals(MojoUtils.TEMPLATE_PROPERTY_QUARKUS_PLATFORM_ARTIFACT_ID_VALUE)
                    && d.getVersion().equals(MojoUtils.TEMPLATE_PROPERTY_QUARKUS_PLATFORM_VERSION_VALUE)
                    && d.getScope().equals("import")
                    && d.getType().equals("pom"))).isTrue();

    assertThat(
            model.getDependencies().stream().anyMatch(d -> d.getArtifactId().equals("quarkus-resteasy")
                    && d.getVersion() == null)).isTrue();
    assertThat(
            model.getDependencies().stream().anyMatch(d -> d.getArtifactId().equals("quarkus-scala")
                    && d.getVersion() == null)).isTrue();

    assertThat(model.getProfiles()).hasSize(1);
    assertThat(model.getProfiles().get(0).getId()).isEqualTo("native");
}
 
Example 14
Source File: KotlinCreateMavenProjectIT.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Test
public void testProjectGenerationFromScratchForKotlin() throws MavenInvocationException, IOException {
    testDir = initEmptyProject("projects/project-generation-kotlin");
    assertThat(testDir).isDirectory();
    invoker = initInvoker(testDir);

    Properties properties = new Properties();
    properties.put("projectGroupId", "org.acme");
    properties.put("projectArtifactId", "acme");
    properties.put("projectVersion", "1.0-SNAPSHOT");
    properties.put("extensions", "kotlin,resteasy-jsonb");
    setup(properties);

    // As the directory is not empty (log) navigate to the artifactID directory
    testDir = new File(testDir, "acme");

    assertThat(new File(testDir, "pom.xml")).isFile();
    assertThat(new File(testDir, "src/main/kotlin")).isDirectory();
    assertThat(new File(testDir, "src/main/resources/application.properties")).isFile();

    String config = Files
            .asCharSource(new File(testDir, "src/main/resources/application.properties"), Charsets.UTF_8)
            .read();
    assertThat(config).contains("key = value");

    assertThat(new File(testDir, "src/main/docker/Dockerfile.native")).isFile();
    assertThat(new File(testDir, "src/main/docker/Dockerfile.jvm")).isFile();

    Model model = loadPom(testDir);
    final DependencyManagement dependencyManagement = model.getDependencyManagement();
    final List<Dependency> dependencies = dependencyManagement.getDependencies();
    assertThat(dependencies.stream()
            .anyMatch(d -> d.getArtifactId().equals(MojoUtils.TEMPLATE_PROPERTY_QUARKUS_PLATFORM_ARTIFACT_ID_VALUE)
                    && d.getVersion().equals(MojoUtils.TEMPLATE_PROPERTY_QUARKUS_PLATFORM_VERSION_VALUE)
                    && d.getScope().equalsIgnoreCase("import")
                    && d.getType().equalsIgnoreCase("pom"))).isTrue();

    assertThat(
            model.getDependencies().stream().anyMatch(d -> d.getArtifactId().equalsIgnoreCase("quarkus-resteasy")
                    && d.getVersion() == null)).isTrue();
    assertThat(
            model.getDependencies().stream().anyMatch(d -> d.getArtifactId().equalsIgnoreCase("quarkus-kotlin")
                    && d.getVersion() == null)).isTrue();

    assertThat(model.getProfiles()).hasSize(1);
    assertThat(model.getProfiles().get(0).getId()).isEqualTo("native");
}
 
Example 15
Source File: QuarkusProjectMojoBase.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private List<Dependency> getManagedDependencies() throws IOException {
    final DependencyManagement managed = project.getModel().getDependencyManagement();
    return managed != null ? managed.getDependencies()
            : Collections.emptyList();
}