1919import org .gradle .api .Task ;
2020import org .gradle .api .file .ArchiveOperations ;
2121import org .gradle .api .plugins .BasePlugin ;
22+ import org .gradle .api .provider .ListProperty ;
23+ import org .gradle .api .provider .Provider ;
2224import org .gradle .api .tasks .Copy ;
2325import org .gradle .api .tasks .TaskProvider ;
2426
@@ -103,19 +105,20 @@ private static TaskProvider<Task> registerCheckMlCppNoticeTask(
103105 ) {
104106 TaskProvider <Task > checkMlCppNoticeTask = project .getTasks ().register ("checkMlCppNotice" , task -> {
105107 task .dependsOn (checkExtraction );
108+ final Provider <Path > noticePath = checkExtraction .map (c ->
109+ c .getDestinationDir ()
110+ .toPath ()
111+ .resolve ("elasticsearch-" + VersionProperties .getElasticsearch () + "/modules/x-pack-ml/NOTICE.txt" ));
112+ ListProperty <String > expectedMlLicenses = extension .expectedMlLicenses ;
106113 task .doLast (new Action <Task >() {
107114 @ Override
108115 public void execute (Task task ) {
109116 // this is just a small sample from the C++ notices,
110117 // 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" );
118+ final List <String > expectedLines = expectedMlLicenses .get ();
116119 final List <String > actualLines ;
117120 try {
118- actualLines = Files .readAllLines (noticePath );
121+ actualLines = Files .readAllLines (noticePath . get () );
119122 for (final String expectedLine : expectedLines ) {
120123 if (actualLines .contains (expectedLine ) == false ) {
121124 throw new GradleException ("expected [" + noticePath + " to contain [" + expectedLine + "] but it did not" );
@@ -133,43 +136,37 @@ public void execute(Task task) {
133136 private TaskProvider <Task > registerCheckNoticeTask (Project project , TaskProvider <Copy > checkExtraction ) {
134137 return project .getTasks ().register ("checkNotice" , task -> {
135138 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- }
139+ var noticePath = checkExtraction .map (
140+ copy -> copy .getDestinationDir ().toPath ().resolve ("elasticsearch-" + VersionProperties .getElasticsearch () + "/NOTICE.txt" )
141+ );
142+ task .doLast (t -> {
143+ final List <String > noticeLines = Arrays .asList ("Elasticsearch" , "Copyright 2009-2024 Elasticsearch" );
144+ assertLinesInFile (noticePath .get (), noticeLines );
146145 });
147146 });
148147 }
149148
150149 private TaskProvider <Task > registerCheckLicenseTask (Project project , TaskProvider <Copy > checkExtraction ) {
151150 TaskProvider <Task > checkLicense = project .getTasks ().register ("checkLicense" , task -> {
152151 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- }
152+ String projectName = project .getName ();
153+ Provider <Path > licensePathProvider = checkExtraction .map (
154+ copy -> copy .getDestinationDir ().toPath ().resolve ("elasticsearch-" + VersionProperties .getElasticsearch () + "/LICENSE.txt" )
155+ );
156+ File rootDir = project .getLayout ().getSettingsDirectory ().getAsFile ();
157+ task .doLast (t -> {
158+ String licenseFilename = null ;
159+ if (projectName .contains ("oss-" ) || projectName .equals ("integ-test-zip" )) {
160+ licenseFilename = "AGPL-3.0+SSPL-1.0+ELASTIC-LICENSE-2.0.txt" ;
161+ } else {
162+ licenseFilename = "ELASTIC-LICENSE-2.0.txt" ;
163+ }
164+ final List <String > licenseLines ;
165+ try {
166+ licenseLines = Files .readAllLines (rootDir .toPath ().resolve ("licenses/" + licenseFilename ));
167+ assertLinesInFile (licensePathProvider .get (), licenseLines );
168+ } catch (IOException ioException ) {
169+ ioException .printStackTrace ();
173170 }
174171 });
175172 });
0 commit comments