1
1
package org .fugerit .java .core .util .mvn ;
2
2
3
+ import java .util .Optional ;
3
4
import java .util .Properties ;
4
5
5
6
import org .fugerit .java .core .util .PropsIO ;
6
7
import org .slf4j .Logger ;
7
8
import org .slf4j .LoggerFactory ;
8
9
10
+ /**
11
+ * Utility class for loading Maven artifact metadata from the classpath.
12
+ * <p>
13
+ * This class provides access to standard Maven properties such as {@code groupId}, {@code artifactId},
14
+ * and {@code version}, as defined in the {@code pom.properties} file bundled with each artifact.
15
+ * </p>
16
+ *
17
+ * @since 8.7.0
18
+ */
9
19
public class MavenProps {
10
20
11
21
private MavenProps () {}
@@ -14,27 +24,65 @@ private MavenProps() {}
14
24
15
25
public static final String VERSION = "version" ;
16
26
17
- public static final String GROUP_ID = "gropupId " ;
27
+ public static final String GROUP_ID = "groupId " ;
18
28
19
29
public static final String ARTIFACT_ID = "artifactId" ;
20
-
30
+
31
+ /**
32
+ * @deprecated It is substituted by {@link #getProperty(String, String, String)} method.
33
+ * @since 8.7.0
34
+ */
35
+ @ Deprecated
21
36
public static String getPropery ( String groupId , String artifactId , String propertyName ) {
37
+ return getProperty ( groupId , artifactId , propertyName );
38
+ }
39
+
40
+ /**
41
+ * Loads the specified Maven property from the classpath.
42
+ *
43
+ * @param groupId the Maven groupId of the dependency
44
+ * @param artifactId the Maven artifactId of the dependency
45
+ * @param propertyName the name of the property to retrieve (e.g. {@code version})
46
+ * @return the property value, or {@code null} if not found
47
+ * @see #getPropertyOptional(String, String, String)
48
+ */
49
+ public static String getProperty ( String groupId , String artifactId , String propertyName ) {
22
50
return loadMavenProps (groupId , artifactId ).getProperty ( propertyName );
23
51
}
52
+
53
+ /**
54
+ * Loads the specified Maven property from the classpath and returns it as an {@link Optional}.
55
+ *
56
+ * @param groupId the Maven groupId of the dependency
57
+ * @param artifactId the Maven artifactId of the dependency
58
+ * @param propertyName the name of the property to retrieve (e.g. {@code version})
59
+ * @return an {@code Optional} containing the property value if found, or {@code Optional.empty()} otherwise
60
+ * @see #getProperty(String, String, String)
61
+ */
62
+ public static Optional <String > getPropertyOptional (String groupId , String artifactId , String propertyName ) {
63
+ return Optional .ofNullable ( loadMavenProps (groupId , artifactId ).getProperty ( propertyName ) );
64
+ }
24
65
25
66
private static final String SEP = "/" ;
26
-
67
+
68
+ /**
69
+ * Loads the {@code pom.properties} file for the specified Maven dependency from the classpath.
70
+ *
71
+ * @param groupId the Maven groupId of the dependency
72
+ * @param artifactId the Maven artifactId of the dependency
73
+ * @return a {@link Properties} object containing the loaded properties,
74
+ * or an empty {@code Properties} object if the file could not be found or loaded
75
+ */
27
76
public static Properties loadMavenProps ( String groupId , String artifactId ) {
28
- Properties props = null ;
29
77
try {
30
- String path = "META-INF/ maven/" + groupId + SEP + artifactId + "/ pom.properties" ;
31
- props = PropsIO .loadFromClassLoader ( path );
78
+ String path = String . join ( SEP , "META-INF" , " maven" , groupId , artifactId , " pom.properties") ;
79
+ Properties props = PropsIO .loadFromClassLoader ( path );
32
80
logger .debug ( "Maven Properties : {}" , props );
81
+ return props ;
33
82
} catch (Exception e ) {
34
83
logger .warn ( "Failed to load props : " +e , e );
35
- props = new Properties ();
84
+ return new Properties ();
36
85
}
37
- return props ;
38
86
}
39
87
40
88
}
0 commit comments