Java Code Examples for org.apache.maven.project.MavenProject#getDependencyManagement()

The following examples show how to use org.apache.maven.project.MavenProject#getDependencyManagement() . 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: BomGeneratorMojo.java    From camel-spring-boot with Apache License 2.0 6 votes vote down vote up
private Set<String> getProvidedDependencyManagement(String groupId, String artifactId, String version, Set<String> gaChecked) throws Exception {
    String ga = groupId + ":" + artifactId;
    gaChecked.add(ga);
    Artifact bom = resolveArtifact(groupId, artifactId, version, "pom");
    MavenProject bomProject = loadExternalProjectPom(bom.getFile());

    Set<String> provided = new HashSet<>();
    if (bomProject.getDependencyManagement() != null && bomProject.getDependencyManagement().getDependencies() != null) {
        for (Dependency dep : bomProject.getDependencyManagement().getDependencies()) {
            if ("pom".equals(dep.getType()) && "import".equals(dep.getScope())) {
                String subGa = dep.getGroupId() + ":" + dep.getArtifactId();
                if (!gaChecked.contains(subGa)) {
                    Set<String> sub = getProvidedDependencyManagement(dep.getGroupId(), dep.getArtifactId(), resolveVersion(bomProject, dep.getVersion()), gaChecked);
                    provided.addAll(sub);
                }
            } else {
                provided.add(comparisonKey(dep));
            }
        }
    }

    return provided;
}
 
Example 2
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 3
Source File: DependenciesMojo.java    From mangooio with Apache License 2.0 6 votes vote down vote up
private void boms(MavenProject project, Properties props) {
    while (project != null) {
        String artifactId = project.getArtifactId();
        if (isBom(artifactId)) {
            props.setProperty(BOMS + artifactId, coordinates(project.getArtifact()));
        }
        if (project.getDependencyManagement() != null) {
            for (Dependency dependency : project.getDependencyManagement().getDependencies()) {
                if ("import".equals(dependency.getScope())) {
                    props.setProperty(BOMS + dependency.getArtifactId(), coordinates(dependency));
                }
            }
        }
        project = project.getParent();
    }
}
 
Example 4
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 5
Source File: MavenTestNGSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean hasEffectiveDependency(String groupId, String artifactId, NbMavenProject prj) {
    MavenProject mp = prj.getMavenProject();
    List<org.apache.maven.model.Dependency> dl = new ArrayList<org.apache.maven.model.Dependency>();
    dl.addAll(mp.getDependencies());
    org.apache.maven.model.DependencyManagement dm = mp.getDependencyManagement();
    if (dm != null) {
        dl.addAll(dm.getDependencies());
    }
    for (org.apache.maven.model.Dependency d : dl) {
        if (groupId.equals(d.getGroupId()) && artifactId.equals(d.getArtifactId())) {
            return true;
        }
    }
    return false;
}
 
Example 6
Source File: CheckDependencyVersions.java    From unleash-maven-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private Set<ArtifactCoordinates> getSnapshotsFromManagement(MavenProject project,
    PomPropertyResolver propertyResolver) {
  this.log.debug("\t\tChecking managed dependencies");
  DependencyManagement dependencyManagement = project.getDependencyManagement();
  if (dependencyManagement != null) {
    Collection<Dependency> snapshots = Collections2.filter(dependencyManagement.getDependencies(),
        new IsSnapshotDependency(propertyResolver));
    HashSet<ArtifactCoordinates> snapshotDependencies = Sets
        .newHashSet(Collections2.transform(snapshots, DependencyToCoordinates.INSTANCE));
    filterMultiModuleDependencies(snapshotDependencies);
    return snapshotDependencies;
  }
  return Collections.emptySet();
}
 
Example 7
Source File: SetupMojoTest.java    From vertx-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void testVerticleCreatPom() throws Exception {

    new File("target/unit/nopom").mkdirs();

    File pomFile = new File("target/unit/nopom/pom.xml");

    String vertxVersion = MojoUtils.getVersion("vertx-core-version");

    Map<String, String> tplContext = new HashMap<>();
    tplContext.put("mProjectGroupId", "com.example.vertx");
    tplContext.put("mProjectArtifactId", "vertx-example");
    tplContext.put("mProjectVersion", "1.0-SNAPSHOT");
    tplContext.put("vertxVersion", vertxVersion);
    tplContext.put("vertxVerticle", "com.example.vertx.MainVerticle");
    tplContext.put("vmpVersion", MojoUtils.getVersion("vertx-maven-plugin-version"));
    SetupTemplateUtils.createPom(tplContext, pomFile);

    assertThat(pomFile).isFile();

    MavenXpp3Reader xpp3Reader = new MavenXpp3Reader();
    Model model = xpp3Reader.read(new FileInputStream(pomFile));
    MavenProject project = new MavenProject(model);
    Optional<Plugin> vmPlugin = MojoUtils.hasPlugin(project, "io.reactiverse:vertx-maven-plugin");
    assertTrue(vmPlugin.isPresent());
    Plugin vmp = project.getPlugin("io.reactiverse:vertx-maven-plugin");
    assertNotNull(vmp);
    Xpp3Dom pluginConfig = (Xpp3Dom) vmp.getConfiguration();
    assertNotNull(pluginConfig);
    String redeploy = pluginConfig.getChild("redeploy").getValue();
    assertNotNull(redeploy);
    assertTrue(Boolean.valueOf(redeploy));
    Properties projectProps = project.getProperties();
    assertNotNull(projectProps);
    assertFalse(projectProps.isEmpty());
    assertEquals(project.getGroupId(), "com.example.vertx");
    assertEquals(project.getArtifactId(), "vertx-example");
    assertEquals(project.getVersion(), "1.0-SNAPSHOT");
    assertEquals(projectProps.getProperty("vertx.version"), vertxVersion);

    assertThat(projectProps.getProperty("vertx-maven-plugin.version"))
        .contains(MojoUtils.getVersion("vertx-maven-plugin-version"));
    assertEquals(projectProps.getProperty("vertx.verticle"), "com.example.vertx.MainVerticle");
    DependencyManagement dependencyManagement = project.getDependencyManagement();
    assertThat(dependencyManagement).isNotNull();
    assertThat(dependencyManagement.getDependencies()).isNotEmpty();
    Dependency vertxBom = project.getDependencyManagement().getDependencies().get(0);
    assertThat(vertxBom.getGroupId()).isEqualTo("io.vertx");
    assertThat(vertxBom.getArtifactId()).isEqualTo("vertx-stack-depchain");
    assertThat(vertxBom.getVersion()).isEqualTo("${vertx.version}");
    assertThat(vertxBom.getType()).isEqualTo("pom");
    assertThat(vertxBom.getScope()).isEqualTo("import");

    assertThat(project.getDependencies()).isNotEmpty();
    Dependency vertxCore =project.getDependencies().get(0);
    assertThat(vertxCore).isNotNull();
    assertThat(vertxCore.getGroupId()).isEqualTo("io.vertx");
    assertThat(vertxCore.getArtifactId()).isEqualTo("vertx-core");
}
 
Example 8
Source File: SetupMojoTest.java    From vertx-maven-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void testVerticleCreatPom() throws Exception {

    new File("target/unit/nopom").mkdirs();

    File pomFile = new File("target/unit/nopom/pom.xml");

    String vertxVersion = MojoUtils.getVersion("vertx-core-version");

    Map<String, String> tplContext = new HashMap<>();
    tplContext.put("mProjectGroupId", "com.example.vertx");
    tplContext.put("mProjectArtifactId", "vertx-example");
    tplContext.put("mProjectVersion", "1.0-SNAPSHOT");
    tplContext.put("vertxVersion", vertxVersion);
    tplContext.put("vertxVerticle", "com.example.vertx.MainVerticle");
    tplContext.put("reactiverseVMPVersion", MojoUtils.getVersion("vertx-maven-plugin-version"));
    SetupTemplateUtils.createPom(tplContext, pomFile);

    assertThat(pomFile).isFile();

    MavenXpp3Reader xpp3Reader = new MavenXpp3Reader();
    Model model = xpp3Reader.read(new FileInputStream(pomFile));
    MavenProject project = new MavenProject(model);
    Optional<Plugin> vmPlugin = MojoUtils.hasPlugin(project, "io.reactiverse:vertx-maven-plugin");
    assertTrue(vmPlugin.isPresent());
    Plugin vmp = project.getPlugin("io.reactiverse:vertx-maven-plugin");
    assertNotNull(vmp);
    Xpp3Dom pluginConfig = (Xpp3Dom) vmp.getConfiguration();
    assertNotNull(pluginConfig);
    String redeploy = pluginConfig.getChild("redeploy").getValue();
    assertNotNull(redeploy);
    assertTrue(Boolean.valueOf(redeploy));
    Properties projectProps = project.getProperties();
    assertNotNull(projectProps);
    assertFalse(projectProps.isEmpty());
    assertEquals(project.getGroupId(), "com.example.vertx");
    assertEquals(project.getArtifactId(), "vertx-example");
    assertEquals(project.getVersion(), "1.0-SNAPSHOT");
    assertEquals(projectProps.getProperty("vertx.version"), vertxVersion);

    assertThat(projectProps.getProperty("vertx-maven-plugin.version"))
        .contains(MojoUtils.getVersion("vertx-maven-plugin-version"));
    assertEquals(projectProps.getProperty("vertx.verticle"), "com.example.vertx.MainVerticle");
    DependencyManagement dependencyManagement = project.getDependencyManagement();
    assertThat(dependencyManagement).isNotNull();
    assertThat(dependencyManagement.getDependencies()).isNotEmpty();
    Dependency vertxBom = project.getDependencyManagement().getDependencies().get(0);
    assertThat(vertxBom.getGroupId()).isEqualTo("io.vertx");
    assertThat(vertxBom.getArtifactId()).isEqualTo("vertx-dependencies");
    assertThat(vertxBom.getVersion()).isEqualTo("${vertx.version}");
    assertThat(vertxBom.getType()).isEqualTo("pom");
    assertThat(vertxBom.getScope()).isEqualTo("import");

    assertThat(project.getDependencies()).isNotEmpty();
    Dependency vertxCore =project.getDependencies().get(0);
    assertThat(vertxCore).isNotNull();
    assertThat(vertxCore.getGroupId()).isEqualTo("io.vertx");
    assertThat(vertxCore.getArtifactId()).isEqualTo("vertx-core");
}