1
1
/*
2
- * Copyright 2012-2019 the original author or authors.
2
+ * Copyright 2012-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
22
22
import java .util .Map ;
23
23
24
24
import org .gradle .api .Project ;
25
+ import org .gradle .api .provider .Property ;
26
+ import org .gradle .api .tasks .Input ;
27
+ import org .gradle .api .tasks .Optional ;
25
28
26
29
/**
27
30
* The properties that are written into the {@code build-info.properties} file.
32
35
@ SuppressWarnings ("serial" )
33
36
public class BuildInfoProperties implements Serializable {
34
37
35
- private final transient Project project ;
38
+ private final Property < String > group ;
36
39
37
- private String group ;
40
+ private final Property < String > artifact ;
38
41
39
- private String artifact ;
42
+ private final Property < String > version ;
40
43
41
- private String version ;
44
+ private final Property < String > name ;
42
45
43
- private String name ;
44
-
45
- private Instant time ;
46
+ private final Property <Instant > time ;
46
47
47
48
private Map <String , Object > additionalProperties = new HashMap <>();
48
49
49
50
BuildInfoProperties (Project project ) {
50
- this .project = project ;
51
- this .time = Instant .now ();
51
+ this .time = project .getObjects ().property (Instant .class );
52
+ this .time .set (Instant .now ());
53
+ this .group = project .getObjects ().property (String .class );
54
+ this .group .set (project .provider (() -> project .getGroup ().toString ()));
55
+ this .artifact = project .getObjects ().property (String .class );
56
+ this .version = project .getObjects ().property (String .class );
57
+ this .version .set (project .provider (() -> project .getVersion ().toString ()));
58
+ this .name = project .getObjects ().property (String .class );
59
+ this .name .set (project .provider (() -> project .getName ().toString ()));
52
60
}
53
61
54
62
/**
55
63
* Returns the value used for the {@code build.group} property. Defaults to the
56
64
* {@link Project#getGroup() Project's group}.
57
65
* @return the group
58
66
*/
67
+ @ Input
68
+ @ Optional
59
69
public String getGroup () {
60
- if (this .group == null ) {
61
- this .group = this .project .getGroup ().toString ();
62
- }
63
- return this .group ;
70
+ return this .group .getOrNull ();
64
71
}
65
72
66
73
/**
67
74
* Sets the value used for the {@code build.group} property.
68
75
* @param group the group name
69
76
*/
70
77
public void setGroup (String group ) {
71
- this .group = group ;
78
+ this .group . set ( group ) ;
72
79
}
73
80
74
81
/**
75
82
* Returns the value used for the {@code build.artifact} property.
76
83
* @return the artifact
77
84
*/
85
+ @ Input
86
+ @ Optional
78
87
public String getArtifact () {
79
- return this .artifact ;
88
+ return this .artifact . getOrNull () ;
80
89
}
81
90
82
91
/**
83
92
* Sets the value used for the {@code build.artifact} property.
84
93
* @param artifact the artifact
85
94
*/
86
95
public void setArtifact (String artifact ) {
87
- this .artifact = artifact ;
96
+ this .artifact . set ( artifact ) ;
88
97
}
89
98
90
99
/**
91
100
* Returns the value used for the {@code build.version} property. Defaults to the
92
101
* {@link Project#getVersion() Project's version}.
93
102
* @return the version
94
103
*/
104
+ @ Input
105
+ @ Optional
95
106
public String getVersion () {
96
- if (this .version == null ) {
97
- this .version = this .project .getVersion ().toString ();
98
- }
99
- return this .version ;
107
+ return this .version .getOrNull ();
100
108
}
101
109
102
110
/**
103
111
* Sets the value used for the {@code build.version} property.
104
112
* @param version the version
105
113
*/
106
114
public void setVersion (String version ) {
107
- this .version = version ;
115
+ this .version . set ( version ) ;
108
116
}
109
117
110
118
/**
111
119
* Returns the value used for the {@code build.name} property. Defaults to the
112
120
* {@link Project#getDisplayName() Project's display name}.
113
121
* @return the name
114
122
*/
123
+ @ Input
124
+ @ Optional
115
125
public String getName () {
116
- if (this .name == null ) {
117
- this .name = this .project .getName ();
118
- }
119
- return this .name ;
126
+ return this .name .getOrNull ();
120
127
}
121
128
122
129
/**
123
130
* Sets the value used for the {@code build.name} property.
124
131
* @param name the name
125
132
*/
126
133
public void setName (String name ) {
127
- this .name = name ;
134
+ this .name . set ( name ) ;
128
135
}
129
136
130
137
/**
131
138
* Returns the value used for the {@code build.time} property. Defaults to
132
139
* {@link Instant#now} when the {@code BuildInfoProperties} instance was created.
133
140
* @return the time
134
141
*/
142
+ @ Input
143
+ @ Optional
135
144
public Instant getTime () {
136
- return this .time ;
145
+ return this .time . getOrNull () ;
137
146
}
138
147
139
148
/**
140
149
* Sets the value used for the {@code build.time} property.
141
150
* @param time the build time
142
151
*/
143
152
public void setTime (Instant time ) {
144
- this .time = time ;
153
+ this .time . set ( time ) ;
145
154
}
146
155
147
156
/**
148
157
* Returns the additional properties that will be included. When written, the name of
149
158
* each additional property is prefixed with {@code build.}.
150
159
* @return the additional properties
151
160
*/
161
+ @ Input
162
+ @ Optional
152
163
public Map <String , Object > getAdditional () {
153
164
return this .additionalProperties ;
154
165
}
@@ -162,46 +173,4 @@ public void setAdditional(Map<String, Object> additionalProperties) {
162
173
this .additionalProperties = additionalProperties ;
163
174
}
164
175
165
- @ Override
166
- public boolean equals (Object obj ) {
167
- if (this == obj ) {
168
- return true ;
169
- }
170
- if (obj == null || getClass () != obj .getClass ()) {
171
- return false ;
172
- }
173
- BuildInfoProperties other = (BuildInfoProperties ) obj ;
174
- boolean result = true ;
175
- result = result && nullSafeEquals (this .additionalProperties , other .additionalProperties );
176
- result = result && nullSafeEquals (this .artifact , other .artifact );
177
- result = result && nullSafeEquals (this .group , other .group );
178
- result = result && nullSafeEquals (this .name , other .name );
179
- result = result && nullSafeEquals (this .version , other .version );
180
- result = result && nullSafeEquals (this .time , other .time );
181
- return result ;
182
- }
183
-
184
- private boolean nullSafeEquals (Object o1 , Object o2 ) {
185
- if (o1 == o2 ) {
186
- return true ;
187
- }
188
- if (o1 == null || o2 == null ) {
189
- return false ;
190
- }
191
- return (o1 .equals (o2 ));
192
- }
193
-
194
- @ Override
195
- public int hashCode () {
196
- final int prime = 31 ;
197
- int result = 1 ;
198
- result = prime * result + ((this .additionalProperties == null ) ? 0 : this .additionalProperties .hashCode ());
199
- result = prime * result + ((this .artifact == null ) ? 0 : this .artifact .hashCode ());
200
- result = prime * result + ((this .group == null ) ? 0 : this .group .hashCode ());
201
- result = prime * result + ((this .name == null ) ? 0 : this .name .hashCode ());
202
- result = prime * result + ((this .version == null ) ? 0 : this .version .hashCode ());
203
- result = prime * result + ((this .time == null ) ? 0 : this .time .hashCode ());
204
- return result ;
205
- }
206
-
207
176
}
0 commit comments