19
19
import org .gradle .api .Task ;
20
20
import org .gradle .api .file .ArchiveOperations ;
21
21
import org .gradle .api .plugins .BasePlugin ;
22
+ import org .gradle .api .provider .ListProperty ;
23
+ import org .gradle .api .provider .Provider ;
22
24
import org .gradle .api .tasks .Copy ;
23
25
import org .gradle .api .tasks .TaskProvider ;
24
26
@@ -103,22 +105,26 @@ private static TaskProvider<Task> registerCheckMlCppNoticeTask(
103
105
) {
104
106
TaskProvider <Task > checkMlCppNoticeTask = project .getTasks ().register ("checkMlCppNotice" , task -> {
105
107
task .dependsOn (checkExtraction );
108
+ final Provider <Path > noticePath = checkExtraction .map (
109
+ c -> c .getDestinationDir ()
110
+ .toPath ()
111
+ .resolve ("elasticsearch-" + VersionProperties .getElasticsearch () + "/modules/x-pack-ml/NOTICE.txt" )
112
+ );
113
+ ListProperty <String > expectedMlLicenses = extension .expectedMlLicenses ;
106
114
task .doLast (new Action <Task >() {
107
115
@ Override
108
116
public void execute (Task task ) {
109
117
// this is just a small sample from the C++ notices,
110
118
// the idea being that if we've added these lines we've probably added all the required lines
111
- final List <String > expectedLines = extension .expectedMlLicenses .get ();
112
- final Path noticePath = checkExtraction .get ()
113
- .getDestinationDir ()
114
- .toPath ()
115
- .resolve ("elasticsearch-" + VersionProperties .getElasticsearch () + "/modules/x-pack-ml/NOTICE.txt" );
119
+ final List <String > expectedLines = expectedMlLicenses .get ();
116
120
final List <String > actualLines ;
117
121
try {
118
- actualLines = Files .readAllLines (noticePath );
122
+ actualLines = Files .readAllLines (noticePath . get () );
119
123
for (final String expectedLine : expectedLines ) {
120
124
if (actualLines .contains (expectedLine ) == false ) {
121
- throw new GradleException ("expected [" + noticePath + " to contain [" + expectedLine + "] but it did not" );
125
+ throw new GradleException (
126
+ "expected [" + noticePath .get () + " to contain [" + expectedLine + "] but it did not"
127
+ );
122
128
}
123
129
}
124
130
} catch (IOException ioException ) {
@@ -133,43 +139,37 @@ public void execute(Task task) {
133
139
private TaskProvider <Task > registerCheckNoticeTask (Project project , TaskProvider <Copy > checkExtraction ) {
134
140
return project .getTasks ().register ("checkNotice" , task -> {
135
141
task .dependsOn (checkExtraction );
136
- task .doLast (new Action <Task >() {
137
- @ Override
138
- public void execute (Task task ) {
139
- final List <String > noticeLines = Arrays .asList ("Elasticsearch" , "Copyright 2009-2024 Elasticsearch" );
140
- final Path noticePath = checkExtraction .get ()
141
- .getDestinationDir ()
142
- .toPath ()
143
- .resolve ("elasticsearch-" + VersionProperties .getElasticsearch () + "/NOTICE.txt" );
144
- assertLinesInFile (noticePath , noticeLines );
145
- }
142
+ var noticePath = checkExtraction .map (
143
+ copy -> copy .getDestinationDir ().toPath ().resolve ("elasticsearch-" + VersionProperties .getElasticsearch () + "/NOTICE.txt" )
144
+ );
145
+ task .doLast (t -> {
146
+ final List <String > noticeLines = Arrays .asList ("Elasticsearch" , "Copyright 2009-2024 Elasticsearch" );
147
+ assertLinesInFile (noticePath .get (), noticeLines );
146
148
});
147
149
});
148
150
}
149
151
150
152
private TaskProvider <Task > registerCheckLicenseTask (Project project , TaskProvider <Copy > checkExtraction ) {
151
153
TaskProvider <Task > checkLicense = project .getTasks ().register ("checkLicense" , task -> {
152
154
task .dependsOn (checkExtraction );
153
- task .doLast (new Action <Task >() {
154
- @ Override
155
- public void execute (Task task ) {
156
- String licenseFilename = null ;
157
- if (project .getName ().contains ("oss-" ) || project .getName ().equals ("integ-test-zip" )) {
158
- licenseFilename = "AGPL-3.0+SSPL-1.0+ELASTIC-LICENSE-2.0.txt" ;
159
- } else {
160
- licenseFilename = "ELASTIC-LICENSE-2.0.txt" ;
161
- }
162
- final List <String > licenseLines ;
163
- try {
164
- licenseLines = Files .readAllLines (project .getRootDir ().toPath ().resolve ("licenses/" + licenseFilename ));
165
- final Path licensePath = checkExtraction .get ()
166
- .getDestinationDir ()
167
- .toPath ()
168
- .resolve ("elasticsearch-" + VersionProperties .getElasticsearch () + "/LICENSE.txt" );
169
- assertLinesInFile (licensePath , licenseLines );
170
- } catch (IOException ioException ) {
171
- ioException .printStackTrace ();
172
- }
155
+ String projectName = project .getName ();
156
+ Provider <Path > licensePathProvider = checkExtraction .map (
157
+ copy -> copy .getDestinationDir ().toPath ().resolve ("elasticsearch-" + VersionProperties .getElasticsearch () + "/LICENSE.txt" )
158
+ );
159
+ File rootDir = project .getLayout ().getSettingsDirectory ().getAsFile ();
160
+ task .doLast (t -> {
161
+ String licenseFilename = null ;
162
+ if (projectName .contains ("oss-" ) || projectName .equals ("integ-test-zip" )) {
163
+ licenseFilename = "AGPL-3.0+SSPL-1.0+ELASTIC-LICENSE-2.0.txt" ;
164
+ } else {
165
+ licenseFilename = "ELASTIC-LICENSE-2.0.txt" ;
166
+ }
167
+ final List <String > licenseLines ;
168
+ try {
169
+ licenseLines = Files .readAllLines (rootDir .toPath ().resolve ("licenses/" + licenseFilename ));
170
+ assertLinesInFile (licensePathProvider .get (), licenseLines );
171
+ } catch (IOException ioException ) {
172
+ ioException .printStackTrace ();
173
173
}
174
174
});
175
175
});
0 commit comments