From 4ad9d7a4caa2dbc02bd96af259e096dc3f5ab13b Mon Sep 17 00:00:00 2001 From: "Matteo Franci a.k.a. Fugerit" Date: Sat, 5 Jul 2025 17:48:34 +0200 Subject: [PATCH 1/3] fj-bom version set to 2.0.2 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80a30a7d..3aa39811 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- fj-bom version set to 2.0.2 + ## [8.6.9] - 2025-04-27 ### Changed From 5d0b7aa08f148e29bedcbc74a11f2b02a9c222be Mon Sep 17 00:00:00 2001 From: "Matteo Franci a.k.a. Fugerit" Date: Sat, 5 Jul 2025 18:01:50 +0200 Subject: [PATCH 2/3] typo in MavenProps utility, constant GROUP_ID value is "gropupId" should be "groupId" #97 --- CHANGELOG.md | 9 +++ .../java/core/util/mvn/MavenProps.java | 64 ++++++++++++++++--- .../java/core/util/mvn/TestMavenProps.java | 11 ++-- 3 files changed, 72 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3aa39811..1a0a363c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,10 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Deprecated + +- MavenProps.getPropery() method. (flagged for removal in future versions) + ### Changed +- new method MavenProps.getPropertyOptional() - fj-bom version set to 2.0.2 +### Fixed + +- typo in MavenProps utility, constant GROUP_ID value is "gropupId" should be "groupId" + ## [8.6.9] - 2025-04-27 ### Changed diff --git a/fj-core/src/main/java/org/fugerit/java/core/util/mvn/MavenProps.java b/fj-core/src/main/java/org/fugerit/java/core/util/mvn/MavenProps.java index 276853eb..0ba60675 100644 --- a/fj-core/src/main/java/org/fugerit/java/core/util/mvn/MavenProps.java +++ b/fj-core/src/main/java/org/fugerit/java/core/util/mvn/MavenProps.java @@ -1,11 +1,21 @@ package org.fugerit.java.core.util.mvn; +import java.util.Optional; import java.util.Properties; import org.fugerit.java.core.util.PropsIO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * Utility class for loading Maven artifact metadata from the classpath. + *

+ * This class provides access to standard Maven properties such as {@code groupId}, {@code artifactId}, + * and {@code version}, as defined in the {@code pom.properties} file bundled with each artifact. + *

+ * + * @since 8.7.0 + */ public class MavenProps { private MavenProps() {} @@ -14,27 +24,65 @@ private MavenProps() {} public static final String VERSION = "version"; - public static final String GROUP_ID = "gropupId"; + public static final String GROUP_ID = "groupId"; public static final String ARTIFACT_ID = "artifactId"; - + + /** + * @deprecated It is substituted by {@link #getProperty(String, String, String)} method. + * @since 8.7.0 + */ + @Deprecated public static String getPropery( String groupId, String artifactId, String propertyName ) { + return getProperty( groupId, artifactId, propertyName ); + } + + /** + * Loads the specified Maven property from the classpath. + * + * @param groupId the Maven groupId of the dependency + * @param artifactId the Maven artifactId of the dependency + * @param propertyName the name of the property to retrieve (e.g. {@code version}) + * @return the property value, or {@code null} if not found + * @see #getPropertyOptional(String, String, String) + */ + public static String getProperty( String groupId, String artifactId, String propertyName ) { return loadMavenProps(groupId, artifactId).getProperty( propertyName ); } + + /** + * Loads the specified Maven property from the classpath and returns it as an {@link Optional}. + * + * @param groupId the Maven groupId of the dependency + * @param artifactId the Maven artifactId of the dependency + * @param propertyName the name of the property to retrieve (e.g. {@code version}) + * @return an {@code Optional} containing the property value if found, or {@code Optional.empty()} otherwise + * @see #getProperty(String, String, String) + */ + public static Optional getPropertyOptional(String groupId, String artifactId, String propertyName ) { + return Optional.ofNullable( loadMavenProps(groupId, artifactId).getProperty( propertyName ) ); + } private static final String SEP = "/"; - + + /** + * Loads the {@code pom.properties} file for the specified Maven dependency from the classpath. + * + * @param groupId the Maven groupId of the dependency + * @param artifactId the Maven artifactId of the dependency + * @return a {@link Properties} object containing the loaded properties, + * or an empty {@code Properties} object if the file could not be found or loaded + */ public static Properties loadMavenProps( String groupId, String artifactId ) { - Properties props = null; try { - String path = "META-INF/maven/"+groupId+SEP+artifactId+"/pom.properties"; - props = PropsIO.loadFromClassLoader( path ); + String path = String.join(SEP, "META-INF", "maven", groupId, artifactId, "pom.properties"); + Properties props = PropsIO.loadFromClassLoader( path ); logger.debug( "Maven Properties : {}", props ); + return props; } catch (Exception e) { logger.warn( "Failed to load props : "+e, e ); - props = new Properties(); + return new Properties(); } - return props; } } diff --git a/fj-core/src/test/java/test/org/fugerit/java/core/util/mvn/TestMavenProps.java b/fj-core/src/test/java/test/org/fugerit/java/core/util/mvn/TestMavenProps.java index e6916403..736d9a23 100644 --- a/fj-core/src/test/java/test/org/fugerit/java/core/util/mvn/TestMavenProps.java +++ b/fj-core/src/test/java/test/org/fugerit/java/core/util/mvn/TestMavenProps.java @@ -6,20 +6,23 @@ import lombok.extern.slf4j.Slf4j; +import java.util.Optional; + @Slf4j class TestMavenProps { @Test void testMavenOk() { - String prop = MavenProps.getPropery( "org.fugerit.java", "fj-core", "artifactId" ); - log.info( "version {}", prop ); - Assertions.assertEquals( "fj-core" , prop ); + Optional prop = MavenProps.getPropertyOptional( "org.fugerit.java", "fj-core", "artifactId" ); + log.info( "version ok {}", prop.get() ); + Assertions.assertEquals( "fj-core" , prop.get() ); } @Test void testMavenKo() { + // use deprecated method for coverage String prop = MavenProps.getPropery( "org.fugerit.java.no.exists", "fj-core", "artifactId" ); - log.info( "version {}", prop ); + log.info( "version ko {}", prop ); Assertions.assertNull( prop ); } From 7f06c61cb6dddc3a23e0e4029473a997ea22c356 Mon Sep 17 00:00:00 2001 From: "Matteo Franci a.k.a. Fugerit" Date: Sat, 5 Jul 2025 18:09:12 +0200 Subject: [PATCH 3/3] feat: utility to check fj-core maven version at runtime --- .../fugerit/java/core/util/mvn/FJCoreMaven.java | 17 +++++++++++++++++ .../java/core/util/mvn/TestMavenProps.java | 12 ++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 fj-core/src/main/java/org/fugerit/java/core/util/mvn/FJCoreMaven.java diff --git a/fj-core/src/main/java/org/fugerit/java/core/util/mvn/FJCoreMaven.java b/fj-core/src/main/java/org/fugerit/java/core/util/mvn/FJCoreMaven.java new file mode 100644 index 00000000..9074415e --- /dev/null +++ b/fj-core/src/main/java/org/fugerit/java/core/util/mvn/FJCoreMaven.java @@ -0,0 +1,17 @@ +package org.fugerit.java.core.util.mvn; + +import java.util.Optional; + +public class FJCoreMaven { + + private FJCoreMaven() {} + + public static final String FJ_CORE_GROUP_ID = "org.fugerit.java"; + + public static final String FJ_CORE_ARTIFACT_ID = "fj-core"; + + public static Optional getFJCoreVersion() { + return MavenProps.getPropertyOptional( FJ_CORE_GROUP_ID, FJ_CORE_ARTIFACT_ID, MavenProps.VERSION ); + } + +} diff --git a/fj-core/src/test/java/test/org/fugerit/java/core/util/mvn/TestMavenProps.java b/fj-core/src/test/java/test/org/fugerit/java/core/util/mvn/TestMavenProps.java index 736d9a23..b902ba18 100644 --- a/fj-core/src/test/java/test/org/fugerit/java/core/util/mvn/TestMavenProps.java +++ b/fj-core/src/test/java/test/org/fugerit/java/core/util/mvn/TestMavenProps.java @@ -1,5 +1,6 @@ package test.org.fugerit.java.core.util.mvn; +import org.fugerit.java.core.util.mvn.FJCoreMaven; import org.fugerit.java.core.util.mvn.MavenProps; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -13,7 +14,7 @@ class TestMavenProps { @Test void testMavenOk() { - Optional prop = MavenProps.getPropertyOptional( "org.fugerit.java", "fj-core", "artifactId" ); + Optional prop = MavenProps.getPropertyOptional(FJCoreMaven.FJ_CORE_GROUP_ID, FJCoreMaven.FJ_CORE_ARTIFACT_ID, MavenProps.ARTIFACT_ID ); log.info( "version ok {}", prop.get() ); Assertions.assertEquals( "fj-core" , prop.get() ); } @@ -21,9 +22,16 @@ void testMavenOk() { @Test void testMavenKo() { // use deprecated method for coverage - String prop = MavenProps.getPropery( "org.fugerit.java.no.exists", "fj-core", "artifactId" ); + String prop = MavenProps.getPropery( "org.fugerit.java.no.exists", FJCoreMaven.FJ_CORE_ARTIFACT_ID, MavenProps.ARTIFACT_ID ); log.info( "version ko {}", prop ); Assertions.assertNull( prop ); } + + @Test + void testFjCoreVersion() { + Optional fjCoreVersion = FJCoreMaven.getFJCoreVersion(); + Assertions.assertTrue( fjCoreVersion.isPresent() ); + log.info( "fjCoreVersion ok {}", fjCoreVersion.get() ); + } }