Skip to content

Commit 1c5e524

Browse files
marcos-lgmaibin
authored andcommitted
Bael 3501 how to create a maven plugin (eugenp#8257)
* BAEL-3501 - maven plugin * formatting * build * maven plugin moved to maven-all module * maven-custom-plugin moved to maven-all * pom fix * format
1 parent 0e8722f commit 1c5e524

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>counter-maven-plugin</artifactId>
7+
<packaging>maven-plugin</packaging>
8+
<version>0.0.1-SNAPSHOT</version>
9+
10+
<name>counter-maven-plugin Maven Mojo</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<organization>
14+
<name>Baeldung</name>
15+
<url>https://www.baeldung.com/</url>
16+
</organization>
17+
18+
<properties>
19+
<maven.compiler.target>1.8</maven.compiler.target>
20+
<maven.compiler.source>1.8</maven.compiler.source>
21+
22+
<maven-plugin-api.version>3.6.2</maven-plugin-api.version>
23+
<maven-plugin-annotations.version>3.6.0</maven-plugin-annotations.version>
24+
<maven-project.version>2.2.1</maven-project.version>
25+
<maven-plugin-plugin.version>3.6.0</maven-plugin-plugin.version>
26+
<maven-site-plugin.version>3.8.2</maven-site-plugin.version>
27+
</properties>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.apache.maven</groupId>
32+
<artifactId>maven-plugin-api</artifactId>
33+
<version>${maven-plugin-api.version}</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.apache.maven.plugin-tools</groupId>
37+
<artifactId>maven-plugin-annotations</artifactId>
38+
<version>${maven-plugin-annotations.version}</version>
39+
<scope>provided</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.apache.maven</groupId>
43+
<artifactId>maven-project</artifactId>
44+
<version>${maven-project.version}</version>
45+
</dependency>
46+
</dependencies>
47+
48+
<build>
49+
<pluginManagement>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.apache.maven.plugins</groupId>
53+
<artifactId>maven-plugin-plugin</artifactId>
54+
<version>${maven-plugin-plugin.version}</version>
55+
</plugin>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-site-plugin</artifactId>
59+
<version>${maven-site-plugin.version}</version>
60+
</plugin>
61+
</plugins>
62+
</pluginManagement>
63+
</build>
64+
65+
<reporting>
66+
<plugins>
67+
<plugin>
68+
<groupId>org.apache.maven.plugins</groupId>
69+
<artifactId>maven-plugin-plugin</artifactId>
70+
<reportSets>
71+
<reportSet>
72+
<reports>
73+
<report>report</report>
74+
</reports>
75+
</reportSet>
76+
</reportSets>
77+
</plugin>
78+
</plugins>
79+
</reporting>
80+
81+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.maven.plugin.validator;
2+
3+
import java.util.List;
4+
5+
import org.apache.maven.model.Dependency;
6+
import org.apache.maven.plugin.AbstractMojo;
7+
import org.apache.maven.plugin.MojoExecutionException;
8+
import org.apache.maven.plugin.MojoFailureException;
9+
import org.apache.maven.plugins.annotations.LifecyclePhase;
10+
import org.apache.maven.plugins.annotations.Mojo;
11+
import org.apache.maven.plugins.annotations.Parameter;
12+
import org.apache.maven.project.MavenProject;
13+
14+
/**
15+
* Counts the number of maven dependencies of a project.
16+
*
17+
* It can be filtered by scope.
18+
*
19+
*/
20+
@Mojo(name = "dependency-counter", defaultPhase = LifecyclePhase.COMPILE)
21+
public class DependencyCounterMojo extends AbstractMojo {
22+
23+
/**
24+
* Scope to filter the dependencies.
25+
*/
26+
@Parameter(property = "scope")
27+
String scope;
28+
29+
/**
30+
* Gives access to the Maven project information.
31+
*/
32+
@Parameter(defaultValue = "${project}", required = true, readonly = true)
33+
MavenProject project;
34+
35+
public void execute() throws MojoExecutionException, MojoFailureException {
36+
List<Dependency> dependencies = project.getDependencies();
37+
38+
long numDependencies = dependencies.stream()
39+
.filter(d -> (scope == null || scope.isEmpty()) || scope.equals(d.getScope()))
40+
.count();
41+
42+
getLog().info("Number of dependencies: " + numDependencies);
43+
}
44+
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>example</artifactId>
8+
<packaging>pom</packaging>
9+
<version>0.0.1-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.apache.commons</groupId>
14+
<artifactId>commons-lang3</artifactId>
15+
<version>3.9</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>4.12</version>
21+
<scope>test</scope>
22+
</dependency>
23+
</dependencies>
24+
25+
<build>
26+
<plugins>
27+
<plugin>
28+
<groupId>com.baeldung</groupId>
29+
<artifactId>counter-maven-plugin</artifactId>
30+
<version>0.0.1-SNAPSHOT</version>
31+
<executions>
32+
<execution>
33+
<goals>
34+
<goal>dependency-counter</goal>
35+
</goals>
36+
</execution>
37+
</executions>
38+
<configuration>
39+
<scope>test</scope>
40+
</configuration>
41+
</plugin>
42+
</plugins>
43+
</build>
44+
45+
</project>

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@
574574
<module>mapstruct</module>
575575
<!-- <module>maven-all/compiler-plugin-java-9</module> --> <!-- We haven't upgraded to java 9. -->
576576
<module>maven-all/maven</module>
577+
<module>maven-all/maven-custom-plugin/counter-maven-plugin</module>
577578
<module>maven-all/maven-war-plugin</module>
578579
<module>maven-all/profiles</module>
579580
<module>maven-all/versions-maven-plugin</module>
@@ -1208,6 +1209,7 @@
12081209
<module>mapstruct</module>
12091210
<!-- <module>maven-all/compiler-plugin-java-9</module> --> <!-- We haven't upgraded to java 9. -->
12101211
<module>maven-all/maven</module>
1212+
<module>maven-all/maven-custom-plugin/counter-maven-plugin</module>
12111213
<module>maven-all/maven-war-plugin</module>
12121214
<module>maven-all/profiles</module>
12131215
<module>maven-all/versions-maven-plugin</module>

0 commit comments

Comments
 (0)