Skip to content

Commit 25c53b6

Browse files
authored
Delete also the directories specified in <targetPath> (#276)
In the list of directories to delete, include the values specified in the `<targetPath>` child of `<source>` elements. Remove the `reportDirectory` field because its value (read-only) was identical to `outputDirectory` (also read-only).
1 parent d1ecf6e commit 25c53b6

File tree

6 files changed

+215
-26
lines changed

6 files changed

+215
-26
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!---
2+
Licensed to the Apache Software Foundation (ASF) under one or more
3+
contributor license agreements. See the NOTICE file distributed with
4+
this work for additional information regarding copyright ownership.
5+
The ASF licenses this file to You under the Apache License, Version 2.0
6+
(the "License"); you may not use this file except in compliance with
7+
the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
The purpose of this file is to verify that the directory specified by `<targetPath>` is deleted.
19+
This verification requires a directory outside the `target` directory, otherwise its deletion could
20+
not be distinguished from the usual deletion of `${maven.build.directory}`.

src/it/sources-targetPath/pom.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
<project xmlns="http://maven.apache.org/POM/4.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.1.0 http://maven.apache.org/xsd/maven-4.1.0.xsd">
21+
<modelVersion>4.1.0</modelVersion>
22+
23+
<groupId>test</groupId>
24+
<artifactId>sources-targetPath</artifactId>
25+
<version>1.0-SNAPSHOT</version>
26+
27+
<name>Test &lt;targetPath&gt; cleaning</name>
28+
<description>Check for proper cleaning of output files in directories specified by &lt;targetPath&gt;.</description>
29+
30+
<build>
31+
<plugins>
32+
<plugin>
33+
<groupId>org.apache.maven.plugins</groupId>
34+
<artifactId>maven-clean-plugin</artifactId>
35+
<version>@pom.version@</version>
36+
</plugin>
37+
</plugins>
38+
39+
<sources>
40+
<source>
41+
<targetPath>${project.basedir}/outside-target</targetPath>
42+
</source>
43+
</sources>
44+
</build>
45+
46+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import java.io.File;
21+
22+
return new File(basedir, "target").exists() && new File(basedir, "outside-target").exists();
23+
// The opposite condition is tested in "verify.bsh".
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!---
2+
Licensed to the Apache Software Foundation (ASF) under one or more
3+
contributor license agreements. See the NOTICE file distributed with
4+
this work for additional information regarding copyright ownership.
5+
The ASF licenses this file to You under the Apache License, Version 2.0
6+
(the "License"); you may not use this file except in compliance with
7+
the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
The purpose of this file is to verify that the directory
19+
specified by `<testOutputDirectory>` is still deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import java.io.File;
21+
22+
return !(new File(basedir, "target").exists() || new File(basedir, "outside-target").exists());

src/main/java/org/apache/maven/plugins/clean/CleanMojo.java

Lines changed: 85 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,21 @@
2020

2121
import java.io.IOException;
2222
import java.nio.file.Path;
23+
import java.util.ArrayList;
24+
import java.util.Arrays;
25+
import java.util.List;
26+
import java.util.Objects;
2327

28+
import org.apache.maven.api.Project;
29+
import org.apache.maven.api.ProjectScope;
2430
import org.apache.maven.api.Session;
2531
import org.apache.maven.api.di.Inject;
32+
import org.apache.maven.api.model.Build;
2633
import org.apache.maven.api.plugin.Log;
2734
import org.apache.maven.api.plugin.MojoException;
2835
import org.apache.maven.api.plugin.annotations.Mojo;
2936
import org.apache.maven.api.plugin.annotations.Parameter;
37+
import org.apache.maven.api.services.ProjectManager;
3038

3139
/**
3240
* Goal which cleans the build.
@@ -52,35 +60,32 @@ public class CleanMojo implements org.apache.maven.api.plugin.Mojo {
5260

5361
public static final String FAST_MODE_DEFER = "defer";
5462

63+
/**
64+
* The logger where to send information about what the plugin is doing.
65+
*/
5566
@Inject
5667
private Log logger;
5768

5869
/**
59-
* This is where build results go.
70+
* The directory where build results went.
6071
*/
6172
@Parameter(defaultValue = "${project.build.directory}", readonly = true, required = true)
6273
private Path directory;
6374

6475
/**
65-
* This is where compiled classes go.
76+
* The directory where compiled main classes went. This is usually a sub-directory
77+
* of {@link #directory}, but could be configured by the user to another location.
6678
*/
6779
@Parameter(defaultValue = "${project.build.outputDirectory}", readonly = true, required = true)
6880
private Path outputDirectory;
6981

7082
/**
71-
* This is where compiled test classes go.
83+
* The directory where compiled test classes went. This is usually a sub-directory
84+
* of {@link #directory}, but could be configured by the user to another location.
7285
*/
7386
@Parameter(defaultValue = "${project.build.testOutputDirectory}", readonly = true, required = true)
7487
private Path testOutputDirectory;
7588

76-
/**
77-
* This is where the site plugin generates its pages.
78-
*
79-
* @since 2.1.1
80-
*/
81-
@Parameter(defaultValue = "${project.build.outputDirectory}", readonly = true, required = true)
82-
private Path reportDirectory;
83-
8489
/**
8590
* Sets whether the plugin runs in verbose mode. As of plugin version 2.3, the default value is derived from Maven's
8691
* global debug flag (compare command line switch {@code -X}).
@@ -218,15 +223,34 @@ public class CleanMojo implements org.apache.maven.api.plugin.Mojo {
218223
@Parameter(property = "maven.clean.fastMode", defaultValue = FAST_MODE_BACKGROUND)
219224
private String fastMode;
220225

226+
/**
227+
* The current session. May be null during some tests.
228+
*/
221229
@Inject
222230
private Session session;
223231

224232
/**
225-
* Deletes file-sets in the following project build directory order: (source) directory, output directory, test
226-
* directory, report directory, and then the additional file-sets.
233+
* The current project instance.
234+
*/
235+
@Inject
236+
private Project project;
237+
238+
/**
239+
* Deletes build directories and file-sets.
240+
* Directories are deleted in the following order:
227241
*
228-
* @throws MojoException When a directory failed to get deleted.
229-
* @see org.apache.maven.api.plugin.Mojo#execute()
242+
* <ol>
243+
* <li>Build directory ({@code ${project.build.directory}})</li>
244+
* <li>Main classes directory ({@code ${project.build.outputDirectory}})</li>
245+
* <li>Test classes directory ({@code ${project.build.testOutputDirectory}})</li>
246+
* <li>Directories specified in the {@code <targetPath>} child of {@code <source>} elements</li>
247+
* <li>Additional file-sets</li>
248+
* </ol>
249+
*
250+
* Redundant directories are omitted. For example, the main classes directory will be skipped
251+
* in the usual case where it is a sub-directory of the build directory.
252+
*
253+
* @throws MojoException if a directory cannot be deleted
230254
*/
231255
@Override
232256
public void execute() {
@@ -263,9 +287,7 @@ public void execute() {
263287
session, logger, isVerbose(), fastDir, fastMode, followSymLinks, force, failOnError, retryOnError);
264288
try {
265289
for (Path directoryItem : getDirectories()) {
266-
if (directoryItem != null) {
267-
cleaner.delete(directoryItem);
268-
}
290+
cleaner.delete(directoryItem);
269291
}
270292
if (filesets != null) {
271293
for (Fileset fileset : filesets) {
@@ -290,16 +312,53 @@ private boolean isVerbose() {
290312
}
291313

292314
/**
293-
* Gets the directories to clean (if any). The returned array may contain null entries.
294-
*
295-
* @return The directories to clean or an empty array if none, never {@code null}.
315+
* {@return the default directories to clean, or an empty list if none}
316+
* The list includes the directories specified in both the Maven 3 and Maven 4 ways.
317+
* Null items and redundant directories (children of other items) are omitted.
296318
*/
297-
private Path[] getDirectories() {
298-
Path[] directories;
319+
private List<Path> getDirectories() {
299320
if (excludeDefaultDirectories) {
300-
directories = new Path[0];
301-
} else {
302-
directories = new Path[] {directory, outputDirectory, testOutputDirectory, reportDirectory};
321+
return List.of();
322+
}
323+
324+
// Directories declared in the Maven 3 way.
325+
var directories = new ArrayList<Path>(Arrays.asList(directory, outputDirectory, testOutputDirectory));
326+
327+
// Directories declared in the Maven 4 way, with <source> elements.
328+
if (project != null && session != null) {
329+
ProjectManager projectManager = session.getService(ProjectManager.class);
330+
if (projectManager != null) {
331+
final Build build = project.getBuild();
332+
projectManager.getSourceRoots(project).forEach((source) -> {
333+
source.targetPath().ifPresent((targetPath) -> {
334+
// TODO: we can replace by `Project.getOutputDirectory(scope)` if MVN-11322 is merged.
335+
String base;
336+
ProjectScope scope = source.scope();
337+
if (scope == ProjectScope.MAIN) {
338+
base = build.getOutputDirectory();
339+
} else if (scope == ProjectScope.TEST) {
340+
base = build.getTestOutputDirectory();
341+
} else {
342+
base = build.getDirectory();
343+
}
344+
Path dir = project.getBasedir().resolve(base);
345+
directories.add(dir.resolve(targetPath));
346+
});
347+
});
348+
}
349+
}
350+
/*
351+
* Remove children of included parents. In the common case where the first element in the list is
352+
* the parent of all other directories, these loops will do only one iteration over all elements.
353+
*/
354+
directories.removeIf(Objects::isNull);
355+
for (int i = 0; i < directories.size(); i++) {
356+
final Path prefix = directories.get(i);
357+
for (int j = directories.size(); --j >= 0; ) {
358+
if (j != i && directories.get(j).startsWith(prefix)) {
359+
directories.remove(j); // Keep only the base directories.
360+
}
361+
}
303362
}
304363
return directories;
305364
}

0 commit comments

Comments
 (0)