Search in sources :

Example 1 with License

use of org.apache.maven.model.License in project asterixdb by apache.

the class LicenseMojo method gatherProjectDependencies.

private void gatherProjectDependencies(MavenProject project, Map<MavenProject, List<Pair<String, String>>> dependencyLicenseMap, Map<String, MavenProject> dependencyGavMap) throws ProjectBuildingException {
    final Set dependencyArtifacts = project.getArtifacts();
    if (dependencyArtifacts != null) {
        for (Object depArtifactObj : dependencyArtifacts) {
            final Artifact depArtifact = (Artifact) depArtifactObj;
            if (!excludedScopes.contains(depArtifact.getScope())) {
                MavenProject dep = resolveDependency(depArtifact);
                dep.setArtifact(depArtifact);
                dependencyGavMap.put(toGav(dep), dep);
                List<Pair<String, String>> licenseUrls = new ArrayList<>();
                for (Object license : dep.getLicenses()) {
                    final License license1 = (License) license;
                    String url = license1.getUrl() != null ? license1.getUrl() : (license1.getName() != null ? license1.getName() : "LICENSE_EMPTY_NAME_URL");
                    licenseUrls.add(new ImmutablePair<>(url, license1.getName()));
                }
                dependencyLicenseMap.put(dep, licenseUrls);
            }
        }
    }
}
Also used : TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) MavenProject(org.apache.maven.project.MavenProject) ArrayList(java.util.ArrayList) License(org.apache.maven.model.License) Artifact(org.apache.maven.artifact.Artifact) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair)

Example 2 with License

use of org.apache.maven.model.License in project maven-plugins by apache.

the class DependenciesRenderer method printDescriptionsAndURLs.

private void printDescriptionsAndURLs(DependencyNode node, String uid) {
    Artifact artifact = node.getArtifact();
    String id = artifact.getId();
    String unknownLicenseMessage = getI18nString("graph.tables.unknown");
    sink.rawText("<div id=\"" + uid + "\" style=\"display:none\">");
    sink.table();
    if (!Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) {
        try {
            MavenProject artifactProject = repoUtils.getMavenProjectFromRepository(artifact);
            String artifactDescription = artifactProject.getDescription();
            String artifactUrl = artifactProject.getUrl();
            String artifactName = artifactProject.getName();
            @SuppressWarnings("unchecked") List<License> licenses = artifactProject.getLicenses();
            sink.tableRow();
            sink.tableHeaderCell();
            sink.text(artifactName);
            sink.tableHeaderCell_();
            sink.tableRow_();
            sink.tableRow();
            sink.tableCell();
            sink.paragraph();
            sink.bold();
            sink.text(getI18nString("column.description") + ": ");
            sink.bold_();
            if (StringUtils.isNotEmpty(artifactDescription)) {
                sink.text(artifactDescription);
            } else {
                sink.text(getI18nString("index", "nodescription"));
            }
            sink.paragraph_();
            if (StringUtils.isNotEmpty(artifactUrl)) {
                sink.paragraph();
                sink.bold();
                sink.text(getI18nString("column.url") + ": ");
                sink.bold_();
                if (ProjectInfoReportUtils.isArtifactUrlValid(artifactUrl)) {
                    sink.link(artifactUrl);
                    sink.text(artifactUrl);
                    sink.link_();
                } else {
                    sink.text(artifactUrl);
                }
                sink.paragraph_();
            }
            sink.paragraph();
            sink.bold();
            sink.text(getI18nString("licenses", "title") + ": ");
            sink.bold_();
            if (!licenses.isEmpty()) {
                for (Iterator<License> it = licenses.iterator(); it.hasNext(); ) {
                    License license = it.next();
                    String licenseName = license.getName();
                    if (StringUtils.isEmpty(licenseName)) {
                        licenseName = getI18nString("unnamed");
                    }
                    String licenseUrl = license.getUrl();
                    if (licenseUrl != null) {
                        sink.link(licenseUrl);
                    }
                    sink.text(licenseName);
                    if (licenseUrl != null) {
                        sink.link_();
                    }
                    if (it.hasNext()) {
                        sink.text(", ");
                    }
                    licenseMap.put(licenseName, artifactName);
                }
            } else {
                sink.text(getI18nString("licenses", "nolicense"));
                licenseMap.put(unknownLicenseMessage, artifactName);
            }
            sink.paragraph_();
        } catch (ProjectBuildingException e) {
            log.warn("Unable to create Maven project from repository for artifact " + artifact.getId(), e);
        }
    } else {
        sink.tableRow();
        sink.tableHeaderCell();
        sink.text(id);
        sink.tableHeaderCell_();
        sink.tableRow_();
        sink.tableRow();
        sink.tableCell();
        sink.paragraph();
        sink.bold();
        sink.text(getI18nString("column.description") + ": ");
        sink.bold_();
        sink.text(getI18nString("index", "nodescription"));
        sink.paragraph_();
        if (artifact.getFile() != null) {
            sink.paragraph();
            sink.bold();
            sink.text(getI18nString("column.url") + ": ");
            sink.bold_();
            sink.text(artifact.getFile().getAbsolutePath());
            sink.paragraph_();
        }
    }
    sink.tableCell_();
    sink.tableRow_();
    sink.table_();
    sink.rawText("</div>");
}
Also used : ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) MavenProject(org.apache.maven.project.MavenProject) License(org.apache.maven.model.License) Artifact(org.apache.maven.artifact.Artifact)

Example 3 with License

use of org.apache.maven.model.License in project maven-plugins by apache.

the class DependenciesRenderer method renderArtifactRow.

/**
     * @param artifact not null
     * @param withClassifier <code>true</code> to include the classifier column, <code>false</code> otherwise.
     * @param withOptional <code>true</code> to include the optional column, <code>false</code> otherwise.
     * @see #getDependencyTableHeader(boolean, boolean)
     */
private void renderArtifactRow(Artifact artifact, boolean withClassifier, boolean withOptional) {
    String isOptional = artifact.isOptional() ? getI18nString("column.isOptional") : getI18nString("column.isNotOptional");
    String url = ProjectInfoReportUtils.getArtifactUrl(artifactFactory, artifact, mavenProjectBuilder, remoteRepositories, localRepository);
    String artifactIdCell = ProjectInfoReportUtils.getArtifactIdCell(artifact.getArtifactId(), url);
    MavenProject artifactProject;
    StringBuilder sb = new StringBuilder();
    try {
        artifactProject = repoUtils.getMavenProjectFromRepository(artifact);
        @SuppressWarnings("unchecked") List<License> licenses = artifactProject.getLicenses();
        for (License license : licenses) {
            sb.append(ProjectInfoReportUtils.getArtifactIdCell(license.getName(), license.getUrl()));
        }
    } catch (ProjectBuildingException e) {
        log.warn("Unable to create Maven project from repository.", e);
    }
    String[] content;
    if (withClassifier) {
        content = new String[] { artifact.getGroupId(), artifactIdCell, artifact.getVersion(), artifact.getClassifier(), artifact.getType(), sb.toString(), isOptional };
    } else {
        content = new String[] { artifact.getGroupId(), artifactIdCell, artifact.getVersion(), artifact.getType(), sb.toString(), isOptional };
    }
    tableRow(withOptional, content);
}
Also used : ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) MavenProject(org.apache.maven.project.MavenProject) License(org.apache.maven.model.License)

Example 4 with License

use of org.apache.maven.model.License in project maven-plugins by apache.

the class LicensesReport method canGenerateReport.

// ----------------------------------------------------------------------
// Public methods
// ----------------------------------------------------------------------
@Override
public boolean canGenerateReport() {
    boolean result = super.canGenerateReport();
    if (result && skipEmptyReport) {
        result = !isEmpty(getProject().getModel().getLicenses());
    }
    if (!result) {
        return false;
    }
    if (!offline) {
        return true;
    }
    for (License license : project.getModel().getLicenses()) {
        String url = license.getUrl();
        URL licenseUrl = null;
        try {
            licenseUrl = getLicenseURL(project, url);
        } catch (MalformedURLException e) {
            getLog().error(e.getMessage());
        } catch (IOException e) {
            getLog().error(e.getMessage());
        }
        if (licenseUrl != null && licenseUrl.getProtocol().equals("file")) {
            return true;
        }
        if (licenseUrl != null && (licenseUrl.getProtocol().equals("http") || licenseUrl.getProtocol().equals("https"))) {
            linkOnly = true;
            return true;
        }
    }
    return false;
}
Also used : MalformedURLException(java.net.MalformedURLException) License(org.apache.maven.model.License) IOException(java.io.IOException) URL(java.net.URL)

Example 5 with License

use of org.apache.maven.model.License in project maven-plugins by apache.

the class DoapUtilTest method testInterpolate.

/**
     * Test method for:
     * {@link DoapUtil#interpolate(String, MavenProject, org.apache.maven.settings.Settings)}
     *
     * @throws Exception if any
     */
public void testInterpolate() throws Exception {
    License license = new License();
    license.setName("licenseName");
    license.setUrl("licenseUrl");
    List<Developer> developers = new ArrayList<Developer>();
    Developer developer1 = new Developer();
    developer1.setId("id1");
    developer1.setName("developerName1");
    developers.add(developer1);
    Developer developer2 = new Developer();
    developer2.setId("id1");
    developer2.setName("developerName2");
    developers.add(developer2);
    MavenProject project = new MavenProject();
    project.setName("projectName");
    project.setDescription("projectDescription");
    project.setLicenses(Collections.singletonList(license));
    project.setDevelopers(developers);
    project.getProperties().put("myKey", "myValue");
    assertEquals(DoapUtil.interpolate("${project.name}", project, null), "projectName");
    assertEquals(DoapUtil.interpolate("my name is ${project.name}", project, null), "my name is projectName");
    assertEquals(DoapUtil.interpolate("my name is ${project.invalid}", project, null), "my name is ${project.invalid}");
    assertEquals(DoapUtil.interpolate("${pom.description}", project, null), "projectDescription");
    assertNull(DoapUtil.interpolate("${project.licenses.name}", project, null));
    assertEquals(DoapUtil.interpolate("${project.licenses[0].name}", project, null), "licenseName");
    assertNull(DoapUtil.interpolate("${project.licenses[1].name}", project, null));
    assertNotNull(DoapUtil.interpolate("${project.developers}", project, null));
    assertEquals(DoapUtil.interpolate("${project.developers[0].name}", project, null), "developerName1");
    assertEquals(DoapUtil.interpolate("${project.developers[1].name}", project, null), "developerName2");
    assertEquals(DoapUtil.interpolate("${myKey}", project, null), "myValue");
}
Also used : MavenProject(org.apache.maven.project.MavenProject) License(org.apache.maven.model.License) ArrayList(java.util.ArrayList) Developer(org.apache.maven.model.Developer)

Aggregations

License (org.apache.maven.model.License)9 MavenProject (org.apache.maven.project.MavenProject)5 Artifact (org.apache.maven.artifact.Artifact)4 ProjectBuildingException (org.apache.maven.project.ProjectBuildingException)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Developer (org.apache.maven.model.Developer)2 Model (org.apache.maven.model.Model)2 Scm (org.apache.maven.model.Scm)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 File (java.io.File)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1