Skip to content

Commit

Permalink
[MCLEAN-123] Switch to Maven 4 and the new api
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jun 24, 2024
1 parent 9073634 commit d17204b
Show file tree
Hide file tree
Showing 16 changed files with 411 additions and 328 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/maven-verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ jobs:
name: Verify
uses: apache/maven-gh-actions-shared/.github/workflows/maven-verify.yml@v4
with:
maven4-enabled: true
ff-maven: "4.0.0-beta-3" # Maven version for fail-fast-build
maven-matrix: '[ "4.0.0-beta-3" ]'
jdk-matrix: '[ "17", "21" ]'
62 changes: 42 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ under the License.
</parent>

<artifactId>maven-clean-plugin</artifactId>
<version>3.4.1-SNAPSHOT</version>
<version>4.0.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>Apache Maven Clean Plugin</name>
Expand Down Expand Up @@ -61,46 +61,44 @@ under the License.
</distributionManagement>

<properties>
<mavenVersion>3.6.3</mavenVersion>
<mavenVersion>4.0.0-beta-3</mavenVersion>
<javaVersion>17</javaVersion>
<version.maven-invoker-plugin>3.7.0</version.maven-invoker-plugin>
<version.maven-plugin-testing>4.0.0-alpha-3-SNAPSHOT</version.maven-plugin-testing>
<version.maven-plugin-tools>4.0.0-SNAPSHOT</version.maven-plugin-tools>
<project.build.outputTimestamp>2024-06-16T10:25:11Z</project.build.outputTimestamp>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<artifactId>maven-api-core</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<artifactId>maven-api-di</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-api</artifactId>
<version>1.1.1</version>
<groupId>org.apache.maven</groupId>
<artifactId>maven-api-meta</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
</dependency>

<!-- dependencies to annotations -->
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<scope>provided</scope>
</dependency>

<!-- Test -->
<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>4.0.0-alpha-2</version>
<version>${version.maven-plugin-testing}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -109,18 +107,42 @@ under the License.
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-testing</artifactId>
<version>1.3.0</version>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${mavenVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-xml</artifactId>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>6.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.13</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>${version.maven-plugin-tools}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>run-its</id>
Expand Down
4 changes: 4 additions & 0 deletions src/it/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ under the License.
<url>@localRepositoryUrl@</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</snapshots>
</repository>
</repositories>
Expand All @@ -44,9 +46,11 @@ under the License.
<url>@localRepositoryUrl@</url>
<releases>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<checksumPolicy>ignore</checksumPolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
Expand Down
70 changes: 40 additions & 30 deletions src/main/java/org/apache/maven/plugins/clean/CleanMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
*/
package org.apache.maven.plugins.clean;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.api.Session;
import org.apache.maven.api.di.Inject;
import org.apache.maven.api.plugin.Log;
import org.apache.maven.api.plugin.MojoException;
import org.apache.maven.api.plugin.annotations.Mojo;
import org.apache.maven.api.plugin.annotations.Parameter;

/**
* Goal which cleans the build.
Expand All @@ -43,40 +45,43 @@
* @see org.apache.maven.plugins.clean.Fileset
* @since 2.0
*/
@Mojo(name = "clean", threadSafe = true)
public class CleanMojo extends AbstractMojo {
@Mojo(name = "clean")
public class CleanMojo implements org.apache.maven.api.plugin.Mojo {

public static final String FAST_MODE_BACKGROUND = "background";

public static final String FAST_MODE_AT_END = "at-end";

public static final String FAST_MODE_DEFER = "defer";

@Inject
private Log logger;

/**
* This is where build results go.
*/
@Parameter(defaultValue = "${project.build.directory}", readonly = true, required = true)
private File directory;
private Path directory;

/**
* This is where compiled classes go.
*/
@Parameter(defaultValue = "${project.build.outputDirectory}", readonly = true, required = true)
private File outputDirectory;
private Path outputDirectory;

/**
* This is where compiled test classes go.
*/
@Parameter(defaultValue = "${project.build.testOutputDirectory}", readonly = true, required = true)
private File testOutputDirectory;
private Path testOutputDirectory;

/**
* This is where the site plugin generates its pages.
*
* @since 2.1.1
*/
@Parameter(defaultValue = "${project.build.outputDirectory}", readonly = true, required = true)
private File reportDirectory;
private Path reportDirectory;

/**
* Sets whether the plugin runs in verbose mode. As of plugin version 2.3, the default value is derived from Maven's
Expand Down Expand Up @@ -186,11 +191,11 @@ public class CleanMojo extends AbstractMojo {
* should usually reside on the same volume. The exact conditions are system dependant though, but if an atomic
* move is not supported, the standard deletion mechanism will be used.
*
* @see #fast
* @since 3.2
* @see #fast
*/
@Parameter(property = "maven.clean.fastDir")
private File fastDir;
private Path fastDir;

/**
* Mode to use when using fast clean. Values are: <code>background</code> to start deletion immediately and
Expand All @@ -199,35 +204,35 @@ public class CleanMojo extends AbstractMojo {
* the actual file deletion should be started in the background when the session ends (this should only be used
* when maven is embedded in a long running process).
*
* @see #fast
* @since 3.2
* @see #fast
*/
@Parameter(property = "maven.clean.fastMode", defaultValue = FAST_MODE_BACKGROUND)
private String fastMode;

@Parameter(defaultValue = "${session}", readonly = true)
private MavenSession session;
@Inject
private Session session;

/**
* Deletes file-sets in the following project build directory order: (source) directory, output directory, test
* directory, report directory, and then the additional file-sets.
*
* @throws MojoExecutionException When a directory failed to get deleted.
* @see org.apache.maven.plugin.Mojo#execute()
* @throws MojoException When a directory failed to get deleted.
* @see org.apache.maven.api.plugin.Mojo#execute()
*/
public void execute() throws MojoExecutionException {
public void execute() {
if (skip) {
getLog().info("Clean is skipped.");
return;
}

String multiModuleProjectDirectory =
session != null ? session.getSystemProperties().getProperty("maven.multiModuleProjectDirectory") : null;
File fastDir;
session != null ? session.getSystemProperties().get("maven.multiModuleProjectDirectory") : null;
Path fastDir;
if (fast && this.fastDir != null) {
fastDir = this.fastDir;
} else if (fast && multiModuleProjectDirectory != null) {
fastDir = new File(multiModuleProjectDirectory, "target/.clean");
fastDir = Paths.get(multiModuleProjectDirectory, "target/.clean");
} else {
fastDir = null;
if (fast) {
Expand All @@ -247,7 +252,7 @@ public void execute() throws MojoExecutionException {
Cleaner cleaner = new Cleaner(session, getLog(), isVerbose(), fastDir, fastMode);

try {
for (File directoryItem : getDirectories()) {
for (Path directoryItem : getDirectories()) {
if (directoryItem != null) {
cleaner.delete(directoryItem, null, followSymLinks, failOnError, retryOnError);
}
Expand All @@ -256,7 +261,7 @@ public void execute() throws MojoExecutionException {
if (filesets != null) {
for (Fileset fileset : filesets) {
if (fileset.getDirectory() == null) {
throw new MojoExecutionException("Missing base directory for " + fileset);
throw new MojoException("Missing base directory for " + fileset);
}
final String[] includes = fileset.getIncludes();
final String[] excludes = fileset.getExcludes();
Expand All @@ -273,8 +278,9 @@ public void execute() throws MojoExecutionException {
fileset.getDirectory(), selector, fileset.isFollowSymlinks(), failOnError, retryOnError);
}
}

} catch (IOException e) {
throw new MojoExecutionException("Failed to clean project: " + e.getMessage(), e);
throw new MojoException("Failed to clean project: " + e.getMessage(), e);
}
}

Expand All @@ -292,13 +298,17 @@ private boolean isVerbose() {
*
* @return The directories to clean or an empty array if none, never <code>null</code>.
*/
private File[] getDirectories() {
File[] directories;
private Path[] getDirectories() {
Path[] directories;
if (excludeDefaultDirectories) {
directories = new File[0];
directories = new Path[0];
} else {
directories = new File[] {directory, outputDirectory, testOutputDirectory, reportDirectory};
directories = new Path[] {directory, outputDirectory, testOutputDirectory, reportDirectory};
}
return directories;
}

private Log getLog() {
return logger;
}
}
Loading

0 comments on commit d17204b

Please sign in to comment.