11package org .fugerit .java .core .util .mvn ;
22
3+ import java .util .Optional ;
34import java .util .Properties ;
45
56import org .fugerit .java .core .util .PropsIO ;
67import org .slf4j .Logger ;
78import org .slf4j .LoggerFactory ;
89
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+ */
919public class MavenProps {
1020
1121 private MavenProps () {}
@@ -14,27 +24,65 @@ private MavenProps() {}
1424
1525 public static final String VERSION = "version" ;
1626
17- public static final String GROUP_ID = "gropupId " ;
27+ public static final String GROUP_ID = "groupId " ;
1828
1929 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
2136 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 ) {
2250 return loadMavenProps (groupId , artifactId ).getProperty ( propertyName );
2351 }
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+ }
2465
2566 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+ */
2776 public static Properties loadMavenProps ( String groupId , String artifactId ) {
28- Properties props = null ;
2977 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 );
3280 logger .debug ( "Maven Properties : {}" , props );
81+ return props ;
3382 } catch (Exception e ) {
3483 logger .warn ( "Failed to load props : " +e , e );
35- props = new Properties ();
84+ return new Properties ();
3685 }
37- return props ;
3886 }
3987
4088}
0 commit comments