diff --git a/maven-all/maven-custom-plugin/counter-maven-plugin/pom.xml b/maven-all/maven-custom-plugin/counter-maven-plugin/pom.xml new file mode 100644 index 0000000000..8cf79bcacb --- /dev/null +++ b/maven-all/maven-custom-plugin/counter-maven-plugin/pom.xml @@ -0,0 +1,81 @@ + + 4.0.0 + com.baeldung + counter-maven-plugin + maven-plugin + 0.0.1-SNAPSHOT + + counter-maven-plugin Maven Mojo + http://maven.apache.org + + + Baeldung + https://www.baeldung.com/ + + + + 1.8 + 1.8 + + 3.6.2 + 3.6.0 + 2.2.1 + 3.6.0 + 3.8.2 + + + + + org.apache.maven + maven-plugin-api + ${maven-plugin-api.version} + + + org.apache.maven.plugin-tools + maven-plugin-annotations + ${maven-plugin-annotations.version} + provided + + + org.apache.maven + maven-project + ${maven-project.version} + + + + + + + + org.apache.maven.plugins + maven-plugin-plugin + ${maven-plugin-plugin.version} + + + org.apache.maven.plugins + maven-site-plugin + ${maven-site-plugin.version} + + + + + + + + + org.apache.maven.plugins + maven-plugin-plugin + + + + report + + + + + + + + \ No newline at end of file diff --git a/maven-all/maven-custom-plugin/counter-maven-plugin/src/main/java/com/baeldung/maven/plugin/validator/DependencyCounterMojo.java b/maven-all/maven-custom-plugin/counter-maven-plugin/src/main/java/com/baeldung/maven/plugin/validator/DependencyCounterMojo.java new file mode 100644 index 0000000000..4bb53b20cf --- /dev/null +++ b/maven-all/maven-custom-plugin/counter-maven-plugin/src/main/java/com/baeldung/maven/plugin/validator/DependencyCounterMojo.java @@ -0,0 +1,45 @@ +package com.baeldung.maven.plugin.validator; + +import java.util.List; + +import org.apache.maven.model.Dependency; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.project.MavenProject; + +/** + * Counts the number of maven dependencies of a project. + * + * It can be filtered by scope. + * + */ +@Mojo(name = "dependency-counter", defaultPhase = LifecyclePhase.COMPILE) +public class DependencyCounterMojo extends AbstractMojo { + + /** + * Scope to filter the dependencies. + */ + @Parameter(property = "scope") + String scope; + + /** + * Gives access to the Maven project information. + */ + @Parameter(defaultValue = "${project}", required = true, readonly = true) + MavenProject project; + + public void execute() throws MojoExecutionException, MojoFailureException { + List dependencies = project.getDependencies(); + + long numDependencies = dependencies.stream() + .filter(d -> (scope == null || scope.isEmpty()) || scope.equals(d.getScope())) + .count(); + + getLog().info("Number of dependencies: " + numDependencies); + } + +} diff --git a/maven-all/maven-custom-plugin/usage-example/pom.xml b/maven-all/maven-custom-plugin/usage-example/pom.xml new file mode 100644 index 0000000000..62c5a64a44 --- /dev/null +++ b/maven-all/maven-custom-plugin/usage-example/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + com.baeldung + example + pom + 0.0.1-SNAPSHOT + + + + org.apache.commons + commons-lang3 + 3.9 + + + junit + junit + 4.12 + test + + + + + + + com.baeldung + counter-maven-plugin + 0.0.1-SNAPSHOT + + + + dependency-counter + + + + + test + + + + + + diff --git a/pom.xml b/pom.xml index 272d198011..7c46f29c74 100644 --- a/pom.xml +++ b/pom.xml @@ -574,6 +574,7 @@ mapstruct maven-all/maven + maven-all/maven-custom-plugin/counter-maven-plugin maven-all/maven-war-plugin maven-all/profiles maven-all/versions-maven-plugin @@ -1208,6 +1209,7 @@ mapstruct maven-all/maven + maven-all/maven-custom-plugin/counter-maven-plugin maven-all/maven-war-plugin maven-all/profiles maven-all/versions-maven-plugin