@@ -34,31 +34,12 @@ public class VersionsPlugin implements Plugin<Project> {
34
34
public void apply (Project project ) {
35
35
final Logger log = project .getLogger ();
36
36
37
- // Expose the version file as an extension
37
+ // Expose the version file as an extension (used during releases)
38
38
final File versionFile = project .getRootProject ().file ( RELATIVE_FILE );
39
39
project .getExtensions ().add ( VERSION_FILE , versionFile );
40
40
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 );
62
43
if ( releaseVersion != null ) {
63
44
log .lifecycle ( "Release version: {} ({})" , releaseVersion .getFullName (), releaseVersion .getFamily () );
64
45
project .getExtensions ().add ( RELEASE_VERSION , releaseVersion );
@@ -67,46 +48,44 @@ public void apply(Project project) {
67
48
log .lifecycle ( "Release version: n/a" );
68
49
}
69
50
51
+ final ProjectVersion developmentVersion = findProjectVersion ( DEVELOPMENT_VERSION , project );
70
52
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 () );
76
54
project .getExtensions ().add ( DEVELOPMENT_VERSION , developmentVersion );
77
55
}
78
56
else {
79
57
log .lifecycle ( "Development version: n/a" );
80
58
}
81
59
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
83
73
final VersionCatalogsExtension catalogs = project .getExtensions ().getByType ( VersionCatalogsExtension .class );
84
74
final VersionCatalog libs = catalogs .named ( "libs" );
85
75
86
- final String ormVersionString = determineOrmVersion ( project , libs );
76
+ final String ormVersionString = determineVersion ( ORM_VERSION , project , libs );
87
77
final Object ormVersion = resolveOrmVersion ( ormVersionString , project );
88
78
log .lifecycle ( "ORM version: {}" , ormVersion );
89
79
project .getExtensions ().add ( ORM_VERSION , ormVersion );
90
80
91
- final Object ormPluginVersion = determineOrmPluginVersion ( ormVersion , project , libs );
81
+ final String ormPluginVersion = determineVersion ( ORM_PLUGIN_VERSION , project , libs );
92
82
log .lifecycle ( "ORM Gradle plugin version: {}" , ormPluginVersion );
93
83
project .getExtensions ().add ( ORM_PLUGIN_VERSION , ormPluginVersion );
94
84
}
95
85
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 );
110
89
if ( version != null ) {
111
90
return new ProjectVersion ( (String ) version );
112
91
}
@@ -115,67 +94,42 @@ private ProjectVersion determineDevelopmentVersion(Project project) {
115
94
}
116
95
117
96
// --- 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 ) {
124
98
if ( releaseVersion != null ) {
125
99
return releaseVersion ;
126
100
}
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)
128
102
final String fullName = projectVersionString .get ();
129
103
if ( fullName .isEmpty () ) {
130
104
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 () );
132
106
}
133
107
return new ProjectVersion ( fullName );
134
108
}
135
109
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 );
143
114
}
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 );
146
118
if ( vc .isPresent () ) {
147
119
final String required = vc .get ().getRequiredVersion ();
148
120
if ( !required .isEmpty () ) {
149
121
return required ;
150
122
}
151
123
}
152
- throw new IllegalStateException ( "Hibernate ORM version not specified on project" );
124
+
125
+ throw new IllegalStateException ( "Property '" + property + "' not found in version catalog" );
153
126
}
154
127
155
128
private Object resolveOrmVersion (String stringForm , Project project ) {
156
129
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 ) ) ) {
159
131
return stringForm ;
160
132
}
161
133
return new ProjectVersion ( stringForm );
162
134
}
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
- }
181
135
}
0 commit comments