Search in sources :

Example 1 with Dependency

use of org.sonar.maven.model.maven2.Dependency in project sonar-java by SonarSource.

the class DependencyWithSystemScopeCheck method scanFile.

@Override
public void scanFile(PomCheckContext context) {
    List<Dependency> dependencies = new MavenDependencyCollector(context.getMavenProject()).allDependencies();
    for (Dependency dependency : dependencies) {
        LocatedAttribute scope = dependency.getScope();
        if (scope != null && "system".equalsIgnoreCase(scope.getValue())) {
            String message = "Update this scope.";
            LocatedAttribute systemPath = dependency.getSystemPath();
            List<PomCheckContext.Location> secondaries = getSecondary(systemPath);
            if (systemPath != null) {
                message = "Update this scope and remove the \"systemPath\".";
            }
            context.reportIssue(this, scope.startLocation().line(), message, secondaries);
        }
    }
}
Also used : MavenDependencyCollector(org.sonar.java.checks.xml.maven.helpers.MavenDependencyCollector) LocatedAttribute(org.sonar.maven.model.LocatedAttribute) Dependency(org.sonar.maven.model.maven2.Dependency)

Example 2 with Dependency

use of org.sonar.maven.model.maven2.Dependency in project sonar-java by SonarSource.

the class PomParserTest method should_retrieve_dependencies_from_dependency_management.

@Test
public void should_retrieve_dependencies_from_dependency_management() {
    MavenProject project = PomParser.parseXML(SIMPLE_POM_FILE);
    DependencyManagement dependencyManagement = project.getDependencyManagement();
    checkPosition(dependencyManagement, 26, 3, 36, 3);
    Dependency dependency = dependencyManagement.getDependencies().getDependencies().get(0);
    checkAttribute(dependency.getGroupId(), "fake", 29, 18, 29, 22);
    checkAttribute(dependency.getArtifactId(), "mock", 30, 21, 30, 25);
    checkAttribute(dependency.getVersion(), "4.0", 31, 18, 31, 21);
    checkAttribute(dependency.getScope(), "system", 32, 16, 32, 22);
    checkAttribute(dependency.getSystemPath(), "hello", 33, 21, 33, 26);
}
Also used : MavenProject(org.sonar.maven.model.maven2.MavenProject) Dependency(org.sonar.maven.model.maven2.Dependency) DependencyManagement(org.sonar.maven.model.maven2.DependencyManagement) Test(org.junit.Test)

Example 3 with Dependency

use of org.sonar.maven.model.maven2.Dependency in project sonar-java by SonarSource.

the class MavenDependencyCollector method fromProfiles.

private static List<Dependency> fromProfiles(@Nullable Profiles profiles) {
    if (profiles != null) {
        List<Dependency> results = new LinkedList<>();
        for (Profile profile : profiles.getProfiles()) {
            results.addAll(fromDependencyManagement(profile.getDependencyManagement()));
            results.addAll(profile.getDependencies() != null ? profile.getDependencies().getDependencies() : Collections.<Dependency>emptyList());
            results.addAll(fromBuild(profile.getBuild()));
        }
        return results;
    }
    return Collections.<Dependency>emptyList();
}
Also used : Dependency(org.sonar.maven.model.maven2.Dependency) LinkedList(java.util.LinkedList) Profile(org.sonar.maven.model.maven2.Profile)

Example 4 with Dependency

use of org.sonar.maven.model.maven2.Dependency in project sonar-java by SonarSource.

the class MavenDependencyMatcherTest method newDependency.

private static Dependency newDependency(String groupId, String artifactId, @Nullable String version) {
    Dependency dependency = new Dependency();
    dependency.setGroupId(new LocatedAttribute(groupId));
    dependency.setArtifactId(new LocatedAttribute(artifactId));
    if (version != null) {
        dependency.setVersion(new LocatedAttribute(version));
    }
    return dependency;
}
Also used : LocatedAttribute(org.sonar.maven.model.LocatedAttribute) Dependency(org.sonar.maven.model.maven2.Dependency)

Example 5 with Dependency

use of org.sonar.maven.model.maven2.Dependency in project sonar-java by SonarSource.

the class MavenDependencyMatcherTest method empty_dependencies_never_match.

@Test
public void empty_dependencies_never_match() {
    matcher = new MavenDependencyMatcher("*:log", "");
    assertNotMatch(new Dependency());
}
Also used : Dependency(org.sonar.maven.model.maven2.Dependency) MavenDependencyMatcher(org.sonar.java.checks.xml.maven.helpers.MavenDependencyMatcher) Test(org.junit.Test)

Aggregations

Dependency (org.sonar.maven.model.maven2.Dependency)5 Test (org.junit.Test)2 LocatedAttribute (org.sonar.maven.model.LocatedAttribute)2 LinkedList (java.util.LinkedList)1 MavenDependencyCollector (org.sonar.java.checks.xml.maven.helpers.MavenDependencyCollector)1 MavenDependencyMatcher (org.sonar.java.checks.xml.maven.helpers.MavenDependencyMatcher)1 DependencyManagement (org.sonar.maven.model.maven2.DependencyManagement)1 MavenProject (org.sonar.maven.model.maven2.MavenProject)1 Profile (org.sonar.maven.model.maven2.Profile)1