@@ -34,31 +34,12 @@ public class VersionsPlugin implements Plugin<Project> {
3434 public void apply (Project project ) {
3535 final Logger log = project .getLogger ();
3636
37- // Expose the version file as an extension
37+ // Expose the version file as an extension (used during releases)
3838 final File versionFile = project .getRootProject ().file ( RELATIVE_FILE );
3939 project .getExtensions ().add ( VERSION_FILE , versionFile );
4040
41- // 1) release/development via -P if they come
42- final ProjectVersion releaseVersion = determineReleaseVersion ( project );
43- final ProjectVersion developmentVersion = determineDevelopmentVersion ( project );
44-
45- // 2) read projectVersion from version.properties using ValueSource (cacheable)
46- final var projectVersionString = project .getProviders ().of (
47- VersionPropertiesSource .class , spec -> {
48- final var rf = project .getLayout ()
49- .getProjectDirectory ()
50- .file ( RELATIVE_FILE );
51- spec .getParameters ().getFile ().set ( rf );
52- }
53- );
54-
55- final ProjectVersion projectVersion = determineProjectVersion (
56- project , releaseVersion , projectVersionString
57- );
58-
59- log .lifecycle ( "Project version: {} ({})" , projectVersion .getFullName (), projectVersion .getFamily () );
60- project .getExtensions ().add ( PROJECT_VERSION , projectVersion );
61-
41+ // 1) Read release/development via -P if they come
42+ final ProjectVersion releaseVersion = findProjectVersion ( RELEASE_VERSION , project );
6243 if ( releaseVersion != null ) {
6344 log .lifecycle ( "Release version: {} ({})" , releaseVersion .getFullName (), releaseVersion .getFamily () );
6445 project .getExtensions ().add ( RELEASE_VERSION , releaseVersion );
@@ -67,46 +48,44 @@ public void apply(Project project) {
6748 log .lifecycle ( "Release version: n/a" );
6849 }
6950
51+ final ProjectVersion developmentVersion = findProjectVersion ( DEVELOPMENT_VERSION , project );
7052 if ( developmentVersion != null ) {
71- log .lifecycle (
72- "Development version: {} ({})" ,
73- developmentVersion .getFullName (),
74- developmentVersion .getFamily ()
75- );
53+ log .lifecycle ( "Development version: {} ({})" , developmentVersion .getFullName (), developmentVersion .getFamily () );
7654 project .getExtensions ().add ( DEVELOPMENT_VERSION , developmentVersion );
7755 }
7856 else {
7957 log .lifecycle ( "Development version: n/a" );
8058 }
8159
82- // 3) Version Catalog ("libs") See local-build-plugins/settings.gradle
60+ // 2) read projectVersion from version.properties using ValueSource (cacheable)
61+ final var projectVersionString = project .getProviders ()
62+ .of ( VersionPropertiesSource .class , spec -> {
63+ final var rf = project .getLayout ().getProjectDirectory ().file ( RELATIVE_FILE );
64+ spec .getParameters ().getFile ().set ( rf );
65+ }
66+ );
67+
68+ final ProjectVersion projectVersion = determineProjectVersion ( project , releaseVersion , projectVersionString );
69+ log .lifecycle ( "Project version: {} ({})" , projectVersion .getFullName (), projectVersion .getFamily () );
70+ project .getExtensions ().add ( PROJECT_VERSION , projectVersion );
71+
72+ // 3) Version Catalog ("libs"): see gradle/libs.versions.toml
8373 final VersionCatalogsExtension catalogs = project .getExtensions ().getByType ( VersionCatalogsExtension .class );
8474 final VersionCatalog libs = catalogs .named ( "libs" );
8575
86- final String ormVersionString = determineOrmVersion ( project , libs );
76+ final String ormVersionString = determineVersion ( ORM_VERSION , project , libs );
8777 final Object ormVersion = resolveOrmVersion ( ormVersionString , project );
8878 log .lifecycle ( "ORM version: {}" , ormVersion );
8979 project .getExtensions ().add ( ORM_VERSION , ormVersion );
9080
91- final Object ormPluginVersion = determineOrmPluginVersion ( ormVersion , project , libs );
81+ final String ormPluginVersion = determineVersion ( ORM_PLUGIN_VERSION , project , libs );
9282 log .lifecycle ( "ORM Gradle plugin version: {}" , ormPluginVersion );
9383 project .getExtensions ().add ( ORM_PLUGIN_VERSION , ormPluginVersion );
9484 }
9585
96- // --- Release / Development (with -P) --------------------------------------
97- private ProjectVersion determineReleaseVersion (Project project ) {
98- if ( project .hasProperty ( RELEASE_VERSION ) ) {
99- final Object version = project .property ( RELEASE_VERSION );
100- if ( version != null ) {
101- return new ProjectVersion ( (String ) version );
102- }
103- }
104- return null ;
105- }
106-
107- private ProjectVersion determineDevelopmentVersion (Project project ) {
108- if ( project .hasProperty ( DEVELOPMENT_VERSION ) ) {
109- final Object version = project .property ( DEVELOPMENT_VERSION );
86+ private ProjectVersion findProjectVersion (String property , Project project ) {
87+ if ( project .hasProperty ( property ) ) {
88+ final Object version = project .property ( property );
11089 if ( version != null ) {
11190 return new ProjectVersion ( (String ) version );
11291 }
@@ -115,67 +94,42 @@ private ProjectVersion determineDevelopmentVersion(Project project) {
11594 }
11695
11796 // --- Project version (ValueSource for configuration cache) ---------------
118-
119- public static ProjectVersion determineProjectVersion (
120- Project project ,
121- ProjectVersion releaseVersion ,
122- Provider <String > projectVersionString
123- ) {
97+ public static ProjectVersion determineProjectVersion (Project project , ProjectVersion releaseVersion , Provider <String > projectVersionString ) {
12498 if ( releaseVersion != null ) {
12599 return releaseVersion ;
126100 }
127- // if don't have an explicit release, use value of file (with ValueSource)
101+ // if we don't have an explicit release, use value from 'version/gradle.properties' (with ValueSource)
128102 final String fullName = projectVersionString .get ();
129103 if ( fullName .isEmpty () ) {
130104 final var file = project .getRootProject ().file ( RELATIVE_FILE );
131- throw new RuntimeException ( "Property 'projectVersion' is missing in " + file .getAbsolutePath () );
105+ throw new RuntimeException ( "Property 'projectVersion' not found in " + file .getAbsolutePath () );
132106 }
133107 return new ProjectVersion ( fullName );
134108 }
135109
136- // --- ORM version from -P or catalogs ------------------------------------
137-
138- private String determineOrmVersion (Project project , VersionCatalog libs ) {
139- // Check if it has been set in the project
140- // -PhibernateOrmVersion have priority
141- if ( project .hasProperty ( ORM_VERSION ) ) {
142- return (String ) project .property ( ORM_VERSION );
110+ private String determineVersion (String property , Project project , VersionCatalog libs ) {
111+ // Check if the property is defined in the project
112+ if ( project .hasProperty ( property ) ) {
113+ return (String ) project .property ( property );
143114 }
144- // Find in Version Catalog
145- final Optional <VersionConstraint > vc = libs .findVersion ( ORM_VERSION );
115+
116+ // Otherwise, look for it in the catalog
117+ final Optional <VersionConstraint > vc = libs .findVersion ( property );
146118 if ( vc .isPresent () ) {
147119 final String required = vc .get ().getRequiredVersion ();
148120 if ( !required .isEmpty () ) {
149121 return required ;
150122 }
151123 }
152- throw new IllegalStateException ( "Hibernate ORM version not specified on project" );
124+
125+ throw new IllegalStateException ( "Property '" + property + "' not found in version catalog" );
153126 }
154127
155128 private Object resolveOrmVersion (String stringForm , Project project ) {
156129 if ( project .hasProperty ( SKIP_ORM_VERSION_PARSING )
157- && Boolean .parseBoolean ( (String ) project .property ( SKIP_ORM_VERSION_PARSING ) )
158- ) {
130+ && Boolean .parseBoolean ( (String ) project .property ( SKIP_ORM_VERSION_PARSING ) ) ) {
159131 return stringForm ;
160132 }
161133 return new ProjectVersion ( stringForm );
162134 }
163-
164- private Object determineOrmPluginVersion (Object ormVersion , Project project , VersionCatalog libs ) {
165- // Check if it has been set in the project
166- // -PhibernateOrmGradlePluginVersion have priority
167- if ( project .hasProperty ( ORM_PLUGIN_VERSION ) ) {
168- return project .property ( ORM_PLUGIN_VERSION );
169- }
170- // Find in Version Catalog
171- final Optional <VersionConstraint > vc = libs .findVersion ( ORM_PLUGIN_VERSION );
172- if ( vc .isPresent () ) {
173- final String required = vc .get ().getRequiredVersion ();
174- if ( !required .isEmpty () ) {
175- return required ;
176- }
177- }
178-
179- throw new IllegalStateException ( "Hibernate ORM Gradle plugin version not specified on project" );
180- }
181135}
0 commit comments