diff --git a/pom.xml b/pom.xml index 8b945de8..c02ab06e 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ build-helper-maven-plugin - 3.6.1-SNAPSHOT + 4.0.0-SNAPSHOT maven-plugin Build Helper Maven Plugin @@ -84,53 +84,26 @@ ${project.build.directory}/staging/build-helper-maven-plugin + 4.0.0-beta-3 + 17 + 4.0.0-beta-1 2024-05-19T19:34:17Z org.apache.maven - maven-artifact + maven-api-core ${mavenVersion} - provided - - - org.apache.maven - maven-core - ${mavenVersion} - provided - - - org.apache.maven - maven-model - ${mavenVersion} - provided - - - org.apache.maven - maven-plugin-api - ${mavenVersion} - provided - - - org.apache.maven - maven-settings - ${mavenVersion} - provided - - - org.apache.maven.resolver - maven-resolver-api - - 1.4.1 - provided + + + + + + + - - org.apache.maven.plugin-tools - maven-plugin-annotations - provided - org.codehaus.plexus plexus-utils @@ -149,7 +122,7 @@ org.slf4j slf4j-api - 1.7.36 + 2.0.13 @@ -166,7 +139,7 @@ org.slf4j slf4j-simple - 1.7.36 + 2.0.13 test diff --git a/src/it/bsh-property/pom.xml b/src/it/bsh-property/pom.xml index d7a98b50..9927e640 100644 --- a/src/it/bsh-property/pom.xml +++ b/src/it/bsh-property/pom.xml @@ -30,10 +30,9 @@ local - name = project.getName(); - root = session.getExecutionRootDirectory(); + name = project.getModel().getName(); + root = session.getRootDirectory(); local = settings.getLocalRepository(); - plexus = session.getContainer(); diff --git a/src/it/remove-project-artifact/invoker.properties b/src/it/remove-project-artifact/invoker.properties deleted file mode 100644 index ac820416..00000000 --- a/src/it/remove-project-artifact/invoker.properties +++ /dev/null @@ -1,5 +0,0 @@ -# first install something -invoker.goals.1 = install - -# next try to delete -invoker.goals.2 = package diff --git a/src/it/remove-project-artifact/pom.xml b/src/it/remove-project-artifact/pom.xml deleted file mode 100644 index f9b421cb..00000000 --- a/src/it/remove-project-artifact/pom.xml +++ /dev/null @@ -1,26 +0,0 @@ - - 4.0.0 - - org.codehaus.mojo - remove-project-artifact-it - 1.0-SNAPSHOT - - - - - @project.groupId@ - @project.artifactId@ - @project.version@ - - - released-version - - remove-project-artifact - - - - - - - - diff --git a/src/it/remove-project-artifact/verify.groovy b/src/it/remove-project-artifact/verify.groovy deleted file mode 100644 index 122decda..00000000 --- a/src/it/remove-project-artifact/verify.groovy +++ /dev/null @@ -1,8 +0,0 @@ -File file = new File(basedir, "build.log"); -assert file.exists(); - -String text = file.getText("utf-8"); - -def FS = File.separator - -assert 2 == text.count("org${FS}codehaus${FS}mojo${FS}remove-project-artifact-it removed.") : 'removed log should be present twice' diff --git a/src/it/rootlocation-in-submodule-deeperlevel-separate-parent/invoker.properties b/src/it/rootlocation-in-submodule-deeperlevel-separate-parent/invoker.properties deleted file mode 100644 index e6f1b830..00000000 --- a/src/it/rootlocation-in-submodule-deeperlevel-separate-parent/invoker.properties +++ /dev/null @@ -1,3 +0,0 @@ -invoker.goals = validate -invoker.buildResult = success -invoker.project = modules/submodule diff --git a/src/it/rootlocation-in-submodule-deeperlevel-separate-parent/modules/submodule/pom.xml b/src/it/rootlocation-in-submodule-deeperlevel-separate-parent/modules/submodule/pom.xml deleted file mode 100644 index a7a57802..00000000 --- a/src/it/rootlocation-in-submodule-deeperlevel-separate-parent/modules/submodule/pom.xml +++ /dev/null @@ -1,17 +0,0 @@ - - 4.0.0 - - - org.codehaus.mojo - build-helper-rootlocation-parent - 1-SNAPSHOT - ../../parent/pom.xml - - - project - jar - - rootlocation sub project - - diff --git a/src/it/rootlocation-in-submodule-deeperlevel-separate-parent/parent/pom.xml b/src/it/rootlocation-in-submodule-deeperlevel-separate-parent/parent/pom.xml deleted file mode 100644 index 270a9014..00000000 --- a/src/it/rootlocation-in-submodule-deeperlevel-separate-parent/parent/pom.xml +++ /dev/null @@ -1,27 +0,0 @@ - - 4.0.0 - - org.codehaus.mojo - build-helper-rootlocation-parent - 1-SNAPSHOT - pom - - - - - @project.groupId@ - @project.artifactId@ - @project.version@ - - - - rootlocation - - - - - - - - diff --git a/src/it/rootlocation-in-submodule-deeperlevel-separate-parent/pom.xml b/src/it/rootlocation-in-submodule-deeperlevel-separate-parent/pom.xml deleted file mode 100644 index c0d6f0d6..00000000 --- a/src/it/rootlocation-in-submodule-deeperlevel-separate-parent/pom.xml +++ /dev/null @@ -1,27 +0,0 @@ - - 4.0.0 - - - org.codehaus.mojo - build-helper-rootlocation-parent - 1-SNAPSHOT - parent/pom.xml - - - org.codehaus.mojo - build-helper-rootlocation-root - 1-SNAPSHOT - pom - - rootlocation search top root - - Tests that rootlocation finds the highest basedir. - - - - parent - modules/submodule - - - diff --git a/src/it/rootlocation-in-submodule-deeperlevel-separate-parent/verify.groovy b/src/it/rootlocation-in-submodule-deeperlevel-separate-parent/verify.groovy deleted file mode 100644 index d52f9903..00000000 --- a/src/it/rootlocation-in-submodule-deeperlevel-separate-parent/verify.groovy +++ /dev/null @@ -1,9 +0,0 @@ -File file = new File( basedir, "build.log" ) -assert file.exists() - -String text = file.getText("utf-8") - -def rootFolderName = (text =~ /(?ms)(.*?)define property rootlocation = "(.*?)[\/\\]([^\/\\"]+)"(.*?)/)[0][3] -assert rootFolderName == "rootlocation-in-submodule-deeperlevel-separate-parent" - -return true diff --git a/src/it/rootlocation-in-submodule-nested-separate-parent/invoker.properties b/src/it/rootlocation-in-submodule-nested-separate-parent/invoker.properties deleted file mode 100644 index e6f1b830..00000000 --- a/src/it/rootlocation-in-submodule-nested-separate-parent/invoker.properties +++ /dev/null @@ -1,3 +0,0 @@ -invoker.goals = validate -invoker.buildResult = success -invoker.project = modules/submodule diff --git a/src/it/rootlocation-in-submodule-nested-separate-parent/modules/pom.xml b/src/it/rootlocation-in-submodule-nested-separate-parent/modules/pom.xml deleted file mode 100644 index ee662c13..00000000 --- a/src/it/rootlocation-in-submodule-nested-separate-parent/modules/pom.xml +++ /dev/null @@ -1,19 +0,0 @@ - - 4.0.0 - - - org.codehaus.mojo - build-helper-rootlocation-parent - 1-SNAPSHOT - ../parent/pom.xml - - - build-helper-rootlocation-modules - pom - - - submodule - - - diff --git a/src/it/rootlocation-in-submodule-nested-separate-parent/modules/submodule/pom.xml b/src/it/rootlocation-in-submodule-nested-separate-parent/modules/submodule/pom.xml deleted file mode 100644 index a7a57802..00000000 --- a/src/it/rootlocation-in-submodule-nested-separate-parent/modules/submodule/pom.xml +++ /dev/null @@ -1,17 +0,0 @@ - - 4.0.0 - - - org.codehaus.mojo - build-helper-rootlocation-parent - 1-SNAPSHOT - ../../parent/pom.xml - - - project - jar - - rootlocation sub project - - diff --git a/src/it/rootlocation-in-submodule-nested-separate-parent/parent/pom.xml b/src/it/rootlocation-in-submodule-nested-separate-parent/parent/pom.xml deleted file mode 100644 index 270a9014..00000000 --- a/src/it/rootlocation-in-submodule-nested-separate-parent/parent/pom.xml +++ /dev/null @@ -1,27 +0,0 @@ - - 4.0.0 - - org.codehaus.mojo - build-helper-rootlocation-parent - 1-SNAPSHOT - pom - - - - - @project.groupId@ - @project.artifactId@ - @project.version@ - - - - rootlocation - - - - - - - - diff --git a/src/it/rootlocation-in-submodule-nested-separate-parent/pom.xml b/src/it/rootlocation-in-submodule-nested-separate-parent/pom.xml deleted file mode 100644 index 2ca2aed9..00000000 --- a/src/it/rootlocation-in-submodule-nested-separate-parent/pom.xml +++ /dev/null @@ -1,27 +0,0 @@ - - 4.0.0 - - - org.codehaus.mojo - build-helper-rootlocation-parent - 1-SNAPSHOT - parent/pom.xml - - - org.codehaus.mojo - build-helper-rootlocation-root - 1-SNAPSHOT - pom - - rootlocation search top root - - Tests that rootlocation finds the highest basedir. - - - - parent - modules - - - diff --git a/src/it/rootlocation-in-submodule-nested-separate-parent/verify.groovy b/src/it/rootlocation-in-submodule-nested-separate-parent/verify.groovy deleted file mode 100644 index 290b6724..00000000 --- a/src/it/rootlocation-in-submodule-nested-separate-parent/verify.groovy +++ /dev/null @@ -1,9 +0,0 @@ -File file = new File( basedir, "build.log" ) -assert file.exists() - -String text = file.getText("utf-8") - -def rootFolderName = (text =~ /(?ms)(.*?)define property rootlocation = "(.*?)[\/\\]([^\/\\"]+)"(.*?)/)[0][3] -assert rootFolderName == "rootlocation-in-submodule-nested-separate-parent" - -return true diff --git a/src/it/rootlocation-in-submodule-separate-parent/invoker.properties b/src/it/rootlocation-in-submodule-separate-parent/invoker.properties deleted file mode 100644 index 965fc19f..00000000 --- a/src/it/rootlocation-in-submodule-separate-parent/invoker.properties +++ /dev/null @@ -1,3 +0,0 @@ -invoker.goals = validate -invoker.buildResult = success -invoker.project = submodule diff --git a/src/it/rootlocation-in-submodule-separate-parent/parent/pom.xml b/src/it/rootlocation-in-submodule-separate-parent/parent/pom.xml deleted file mode 100644 index 270a9014..00000000 --- a/src/it/rootlocation-in-submodule-separate-parent/parent/pom.xml +++ /dev/null @@ -1,27 +0,0 @@ - - 4.0.0 - - org.codehaus.mojo - build-helper-rootlocation-parent - 1-SNAPSHOT - pom - - - - - @project.groupId@ - @project.artifactId@ - @project.version@ - - - - rootlocation - - - - - - - - diff --git a/src/it/rootlocation-in-submodule-separate-parent/pom.xml b/src/it/rootlocation-in-submodule-separate-parent/pom.xml deleted file mode 100644 index 922e02c3..00000000 --- a/src/it/rootlocation-in-submodule-separate-parent/pom.xml +++ /dev/null @@ -1,27 +0,0 @@ - - 4.0.0 - - - org.codehaus.mojo - build-helper-rootlocation-parent - 1-SNAPSHOT - parent/pom.xml - - - org.codehaus.mojo - build-helper-rootlocation-root - 1-SNAPSHOT - pom - - rootlocation search top root - - Tests that rootlocation finds the highest basedir. - - - - parent - submodule - - - diff --git a/src/it/rootlocation-in-submodule-separate-parent/submodule/pom.xml b/src/it/rootlocation-in-submodule-separate-parent/submodule/pom.xml deleted file mode 100644 index 610c201f..00000000 --- a/src/it/rootlocation-in-submodule-separate-parent/submodule/pom.xml +++ /dev/null @@ -1,17 +0,0 @@ - - 4.0.0 - - - org.codehaus.mojo - build-helper-rootlocation-parent - 1-SNAPSHOT - ../parent/pom.xml - - - project - jar - - rootlocation sub project - - diff --git a/src/it/rootlocation-in-submodule-separate-parent/verify.groovy b/src/it/rootlocation-in-submodule-separate-parent/verify.groovy deleted file mode 100644 index 133d0e5f..00000000 --- a/src/it/rootlocation-in-submodule-separate-parent/verify.groovy +++ /dev/null @@ -1,9 +0,0 @@ -File file = new File( basedir, "build.log" ) -assert file.exists() - -String text = file.getText("utf-8") - -def rootFolderName = (text =~ /(?ms)(.*?)define property rootlocation = "(.*?)[\/\\]([^\/\\"]+)"(.*?)/)[0][3] -assert rootFolderName == "rootlocation-in-submodule-separate-parent" - -return true diff --git a/src/it/rootlocation-in-submodule/invoker.properties b/src/it/rootlocation-in-submodule/invoker.properties deleted file mode 100644 index 965fc19f..00000000 --- a/src/it/rootlocation-in-submodule/invoker.properties +++ /dev/null @@ -1,3 +0,0 @@ -invoker.goals = validate -invoker.buildResult = success -invoker.project = submodule diff --git a/src/it/rootlocation-in-submodule/pom.xml b/src/it/rootlocation-in-submodule/pom.xml deleted file mode 100644 index 41b0646e..00000000 --- a/src/it/rootlocation-in-submodule/pom.xml +++ /dev/null @@ -1,36 +0,0 @@ - - 4.0.0 - - org.codehaus.mojo - build-helper-rootlocation-parent - 1-SNAPSHOT - pom - - rootlocation search top root - - Tests that rootlocation finds the highest basedir. - - - - submodule - - - - - - @project.groupId@ - @project.artifactId@ - @project.version@ - - - - rootlocation - - - - - - - - diff --git a/src/it/rootlocation-in-submodule/submodule/pom.xml b/src/it/rootlocation-in-submodule/submodule/pom.xml deleted file mode 100644 index f3ac5ef4..00000000 --- a/src/it/rootlocation-in-submodule/submodule/pom.xml +++ /dev/null @@ -1,16 +0,0 @@ - - 4.0.0 - - - org.codehaus.mojo - build-helper-rootlocation-parent - 1-SNAPSHOT - - - project - jar - - rootlocation sub project - - diff --git a/src/it/rootlocation-in-submodule/verify.groovy b/src/it/rootlocation-in-submodule/verify.groovy deleted file mode 100644 index fa263525..00000000 --- a/src/it/rootlocation-in-submodule/verify.groovy +++ /dev/null @@ -1,9 +0,0 @@ -File file = new File( basedir, "build.log" ) -assert file.exists() - -String text = file.getText("utf-8") - -def rootFolderName = (text =~ /(?ms)(.*?)define property rootlocation = "(.*?)[\/\\]([^\/\\"]+)"(.*?)/)[0][3] -assert rootFolderName == "rootlocation-in-submodule" - -return true diff --git a/src/it/rootlocation-run-only-at-exec-root/invoker.properties b/src/it/rootlocation-run-only-at-exec-root/invoker.properties deleted file mode 100644 index 877b4904..00000000 --- a/src/it/rootlocation-run-only-at-exec-root/invoker.properties +++ /dev/null @@ -1,2 +0,0 @@ -invoker.goals = validate -invoker.buildResult = success diff --git a/src/it/rootlocation-run-only-at-exec-root/pom.xml b/src/it/rootlocation-run-only-at-exec-root/pom.xml deleted file mode 100644 index 0492b34b..00000000 --- a/src/it/rootlocation-run-only-at-exec-root/pom.xml +++ /dev/null @@ -1,39 +0,0 @@ - - 4.0.0 - - org.codehaus.mojo - build-helper-rootlocation-parent - 1-SNAPSHOT - pom - - rootlocation run only at execution root - - Tests that rootlocation is executed only at execution top level. - - - - submodule - - - - - - @project.groupId@ - @project.artifactId@ - @project.version@ - - - - rootlocation - - - true - - - - - - - - diff --git a/src/it/rootlocation-run-only-at-exec-root/submodule/pom.xml b/src/it/rootlocation-run-only-at-exec-root/submodule/pom.xml deleted file mode 100644 index f3ac5ef4..00000000 --- a/src/it/rootlocation-run-only-at-exec-root/submodule/pom.xml +++ /dev/null @@ -1,16 +0,0 @@ - - 4.0.0 - - - org.codehaus.mojo - build-helper-rootlocation-parent - 1-SNAPSHOT - - - project - jar - - rootlocation sub project - - diff --git a/src/it/rootlocation-run-only-at-exec-root/verify.groovy b/src/it/rootlocation-run-only-at-exec-root/verify.groovy deleted file mode 100644 index ed9f620b..00000000 --- a/src/it/rootlocation-run-only-at-exec-root/verify.groovy +++ /dev/null @@ -1,11 +0,0 @@ -File file = new File( basedir, "build.log" ) -assert file.exists() - -String text = file.getText("utf-8") - -def rootFolderName = (text =~ /(?ms)(.*?)define property rootlocation = "(.*?)[\/\\]([^\/\\"]+)"(.*?)/)[0][3] -assert rootFolderName == "rootlocation-run-only-at-exec-root" - -assert text.contains("Skip getting the rootlocation in this project because it's not the Execution Root") - -return true diff --git a/src/it/rootlocation/invoker.properties b/src/it/rootlocation/invoker.properties deleted file mode 100644 index 877b4904..00000000 --- a/src/it/rootlocation/invoker.properties +++ /dev/null @@ -1,2 +0,0 @@ -invoker.goals = validate -invoker.buildResult = success diff --git a/src/it/rootlocation/pom.xml b/src/it/rootlocation/pom.xml deleted file mode 100644 index ef682e63..00000000 --- a/src/it/rootlocation/pom.xml +++ /dev/null @@ -1,32 +0,0 @@ - - 4.0.0 - - org.codehaus.mojo - build-helper-rootlocation - 1-SNAPSHOT - jar - - rootlocation - - Tests that rootlocation is set. - - - - - - @project.groupId@ - @project.artifactId@ - @project.version@ - - - - rootlocation - - - - - - - - diff --git a/src/it/rootlocation/verify.groovy b/src/it/rootlocation/verify.groovy deleted file mode 100644 index 9d631cf1..00000000 --- a/src/it/rootlocation/verify.groovy +++ /dev/null @@ -1,9 +0,0 @@ -File file = new File( basedir, "build.log" ) -assert file.exists() - -String text = file.getText("utf-8") - -def rootFolderName = (text =~ /(?ms)(.*?)define property rootlocation = "(.*?)[\/\\]([^\/\\"]+)"(.*?)/)[0][3] -assert rootFolderName == "rootlocation" - -return true diff --git a/src/main/java/org/codehaus/mojo/buildhelper/AbstractAddResourceMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/AbstractAddResourceMojo.java index 5503d604..122ca776 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/AbstractAddResourceMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/AbstractAddResourceMojo.java @@ -24,12 +24,16 @@ * SOFTWARE. */ -import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; -import org.apache.maven.model.Resource; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.project.MavenProject; +import org.apache.maven.api.Project; +import org.apache.maven.api.ProjectScope; +import org.apache.maven.api.Session; +import org.apache.maven.api.di.Inject; +import org.apache.maven.api.plugin.annotations.Parameter; +import org.apache.maven.api.services.ProjectManager; +import org.codehaus.mojo.buildhelper.utils.Resource; /** * Abstract Mojo for adding Resources @@ -44,8 +48,11 @@ public abstract class AbstractAddResourceMojo extends AbstractMojo { /** * The maven project */ - @Parameter(readonly = true, defaultValue = "${project}") - private MavenProject project; + @Inject + private Project project; + + @Inject + private Session session; /** * Main plugin execution @@ -61,13 +68,10 @@ public void execute() { for (Resource resource : resources) { // Check for relative paths in the resource configuration. // http://maven.apache.org/plugin-developers/common-bugs.html#Resolving_Relative_Paths - File resourceDir = new File(resource.getDirectory()); - if (!resourceDir.isAbsolute()) { - resourceDir = new File(project.getBasedir(), resource.getDirectory()); - resource.setDirectory(resourceDir.getAbsolutePath()); - } + Path resourceDir = project.getBasedir().resolve(resource.getDirectory()); + resource.setDirectory(resourceDir.toAbsolutePath().toString()); - if (isSkipIfMissing() && !resourceDir.exists()) { + if (isSkipIfMissing() && !Files.exists(resourceDir)) { if (getLog().isDebugEnabled()) { getLog().debug("Skipping directory: " + resourceDir + ", because it does not exist."); } @@ -77,23 +81,41 @@ public void execute() { } } + public void addResource(Resource resource) { + getProjectManager().addResource(getProject(), getProjectScope(), newResource(resource)); + if (getLog().isDebugEnabled()) { + getLog().debug((getProjectScope() == ProjectScope.MAIN ? "Added resource: " : "Added test resource: ") + + resource.getDirectory()); + } + } + + static org.apache.maven.api.model.Resource newResource(Resource res) { + return org.apache.maven.api.model.Resource.newBuilder() + .directory(res.getDirectory()) + .filtering(Boolean.toString(res.isFiltering())) + .excludes(res.getExcludes()) + .includes(res.getIncludes()) + .mergeId(res.getMergeId()) + .targetPath(res.getTargetPath()) + .build(); + } + protected abstract boolean isSkipIfMissing(); protected abstract boolean isSkip(); - /** - * Add the resource to the project. - * - * @param resource the resource to add - */ - public abstract void addResource(Resource resource); + protected abstract ProjectScope getProjectScope(); /** * Get the current project instance. * * @return the project */ - public MavenProject getProject() { + public Project getProject() { return this.project; } + + public ProjectManager getProjectManager() { + return session.getService(ProjectManager.class); + } } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/AbstractDefinePropertyMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/AbstractDefinePropertyMojo.java index 60bb4c19..c4d625e0 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/AbstractDefinePropertyMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/AbstractDefinePropertyMojo.java @@ -24,23 +24,28 @@ * SOFTWARE. */ -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.project.MavenProject; +import org.apache.maven.api.Project; +import org.apache.maven.api.Session; +import org.apache.maven.api.di.Inject; +import org.apache.maven.api.services.ProjectManager; public abstract class AbstractDefinePropertyMojo extends AbstractMojo { /** * The maven project */ - @Parameter(readonly = true, defaultValue = "${project}") - protected MavenProject project; + @Inject + protected Project project; + + @Inject + protected Session session; protected void defineProperty(String name, String value) { if (getLog().isDebugEnabled()) { getLog().debug("define property " + name + " = \"" + value + "\""); } - project.getProperties().put(name, value); + ProjectManager projectManager = session.getService(ProjectManager.class); + projectManager.setProperty(project, name, value); } /** @@ -48,7 +53,7 @@ protected void defineProperty(String name, String value) { * * @return the project */ - public MavenProject getProject() { + public Project getProject() { return this.project; } } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/AbstractMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/AbstractMojo.java new file mode 100644 index 00000000..5d4fb347 --- /dev/null +++ b/src/main/java/org/codehaus/mojo/buildhelper/AbstractMojo.java @@ -0,0 +1,15 @@ +package org.codehaus.mojo.buildhelper; + +import org.apache.maven.api.di.Inject; +import org.apache.maven.api.plugin.Log; +import org.apache.maven.api.plugin.Mojo; + +public abstract class AbstractMojo implements Mojo { + + @Inject + Log log; + + public Log getLog() { + return log; + } +} diff --git a/src/main/java/org/codehaus/mojo/buildhelper/AbstractRegexPropertyMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/AbstractRegexPropertyMojo.java index 6e6803e6..9baf5620 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/AbstractRegexPropertyMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/AbstractRegexPropertyMojo.java @@ -29,24 +29,23 @@ import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.api.plugin.MojoException; import org.codehaus.plexus.util.StringUtils; public abstract class AbstractRegexPropertyMojo extends AbstractDefinePropertyMojo { - protected void execute(RegexPropertySetting config) throws MojoExecutionException, MojoFailureException { + protected void execute(RegexPropertySetting config) throws MojoException { try { config.validate(); } catch (IllegalArgumentException e) { - throw new MojoExecutionException(e.getMessage(), e); + throw new MojoException(e.getMessage(), e); } Pattern pattern; try { pattern = Pattern.compile(config.getRegex()); } catch (PatternSyntaxException e) { - throw new MojoExecutionException(e.getMessage(), e); + throw new MojoException(e.getMessage(), e); } Matcher matcher = pattern.matcher(config.getValue()); @@ -58,7 +57,7 @@ protected void execute(RegexPropertySetting config) throws MojoExecutionExceptio : matcher.replaceAll(""))); } else { if (config.isFailIfNoMatch()) { - throw new MojoFailureException( + throw new MojoException( "No match to regex '" + config.getRegex() + "' found in '" + config.getValue() + "'."); } else { getLog().info("No match to regex '" + config.getRegex() + "' found in '" + config.getValue() + "'. " diff --git a/src/main/java/org/codehaus/mojo/buildhelper/AbstractUpToDatePropertyMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/AbstractUpToDatePropertyMojo.java index 1fef291f..6b6b2fc0 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/AbstractUpToDatePropertyMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/AbstractUpToDatePropertyMojo.java @@ -23,8 +23,7 @@ import java.io.IOException; import java.util.Map; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.api.plugin.MojoException; import org.apache.maven.shared.model.fileset.FileSet; import org.apache.maven.shared.model.fileset.mappers.MapperException; import org.apache.maven.shared.model.fileset.util.FileSetManager; @@ -44,11 +43,11 @@ abstract class AbstractUpToDatePropertyMojo extends AbstractDefinePropertyMojo { protected AbstractUpToDatePropertyMojo() {} - protected void execute(UpToDatePropertySetting config) throws MojoExecutionException, MojoFailureException { + protected void execute(UpToDatePropertySetting config) throws MojoException { try { config.validate(); } catch (IllegalArgumentException e) { - throw new MojoExecutionException("Invalid UpToDateProperty configuration", e); + throw new MojoException("Invalid UpToDateProperty configuration", e); } // Check that all target file(s) are up to date with respect to their corresponding source files. @@ -91,7 +90,7 @@ protected void execute(UpToDatePropertySetting config) throws MojoExecutionExcep return isUpToDate; }); } catch (MapperException e) { - throw new MojoExecutionException("", e); + throw new MojoException("", e); } } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/AddResourceMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/AddResourceMojo.java index 897c888c..7d6ef234 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/AddResourceMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/AddResourceMojo.java @@ -24,10 +24,9 @@ * SOFTWARE. */ -import org.apache.maven.model.Resource; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.api.ProjectScope; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; /** * Add more resource directories to the POM. @@ -35,7 +34,7 @@ * @author Paul Gier * @since 1.3 */ -@Mojo(name = "add-resource", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true) +@Mojo(name = "add-resource", defaultPhase = "generate-resources") public class AddResourceMojo extends AbstractAddResourceMojo { /** @@ -54,18 +53,18 @@ public class AddResourceMojo extends AbstractAddResourceMojo { @Parameter(property = "buildhelper.addresource.skipIfMissing", defaultValue = "false") private boolean skipAddResourceIfMissing; - public void addResource(Resource resource) { - getProject().addResource(resource); - if (getLog().isDebugEnabled()) { - getLog().debug("Added resource: " + resource.getDirectory()); - } - } - + @Override protected boolean isSkipIfMissing() { return skipAddResourceIfMissing; } + @Override protected boolean isSkip() { return skipAddResource; } + + @Override + protected ProjectScope getProjectScope() { + return ProjectScope.MAIN; + } } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/AddSourceMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/AddSourceMojo.java index d74c4b9d..308890b7 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/AddSourceMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/AddSourceMojo.java @@ -24,13 +24,16 @@ * SOFTWARE. */ -import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.project.MavenProject; +import org.apache.maven.api.Project; +import org.apache.maven.api.ProjectScope; +import org.apache.maven.api.Session; +import org.apache.maven.api.di.Inject; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; +import org.apache.maven.api.services.ProjectManager; /** * Add more source directories to the POM. @@ -38,21 +41,16 @@ * @author Dan T. Tran * @since 1.0 */ -@Mojo(name = "add-source", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true) +@Mojo(name = "add-source", defaultPhase = "generate-sources") public class AddSourceMojo extends AbstractMojo { + /** * Additional source directories. * * @since 1.0 */ @Parameter(property = "sources", required = true) - private File[] sources; - - /** - * @since 1.0 - */ - @Parameter(readonly = true, defaultValue = "${project}") - private MavenProject project; + private Path[] sources; /** * Skip plugin execution. @@ -70,6 +68,12 @@ public class AddSourceMojo extends AbstractMojo { @Parameter(property = "buildhelper.addsource.skipIfMissing", defaultValue = "false") private boolean skipAddSourceIfMissing; + @Inject + private Project project; + + @Inject + private Session session; + public void execute() { if (skipAddSource) { if (getLog().isInfoEnabled()) { @@ -78,13 +82,14 @@ public void execute() { return; } - for (File source : sources) { - if (skipAddSourceIfMissing && !source.exists()) { + for (Path source : sources) { + if (skipAddSourceIfMissing && !Files.exists(source)) { if (getLog().isDebugEnabled()) { getLog().debug("Skipping directory: " + source + ", because it does not exist."); } } else { - this.project.addCompileSourceRoot(source.getAbsolutePath()); + ProjectManager projectManager = session.getService(ProjectManager.class); + projectManager.addCompileSourceRoot(project, ProjectScope.MAIN, source.toAbsolutePath()); if (getLog().isInfoEnabled()) { getLog().info("Source directory: " + source + " added."); } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/AddTestResourceMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/AddTestResourceMojo.java index df107c19..10a0c5bc 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/AddTestResourceMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/AddTestResourceMojo.java @@ -24,10 +24,9 @@ * SOFTWARE. */ -import org.apache.maven.model.Resource; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.api.ProjectScope; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; /** * Add more test resource directories to the POM. @@ -35,7 +34,7 @@ * @author Paul Gier * @since 1.3 */ -@Mojo(name = "add-test-resource", defaultPhase = LifecyclePhase.GENERATE_TEST_RESOURCES, threadSafe = true) +@Mojo(name = "add-test-resource", defaultPhase = "generate-test-resources") public class AddTestResourceMojo extends AbstractAddResourceMojo { /** @@ -54,24 +53,18 @@ public class AddTestResourceMojo extends AbstractAddResourceMojo { @Parameter(property = "buildhelper.addtestresource.skipIfMissing", defaultValue = "false") private boolean skipAddTestResourceIfMissing; - /** - * Add the resource to the project. - * - * @param resource the resource to add - */ - public void addResource(Resource resource) { - getProject().addTestResource(resource); - if (getLog().isDebugEnabled()) { - getLog().debug("Added test resource: " + resource.getDirectory()); - } - } - @Override protected boolean isSkip() { return skipAddTestResource; } + @Override protected boolean isSkipIfMissing() { return skipAddTestResourceIfMissing; } + + @Override + protected ProjectScope getProjectScope() { + return ProjectScope.TEST; + } } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/AddTestSourceMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/AddTestSourceMojo.java index 0251a76a..602ff506 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/AddTestSourceMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/AddTestSourceMojo.java @@ -24,13 +24,16 @@ * SOFTWARE. */ -import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.project.MavenProject; +import org.apache.maven.api.Project; +import org.apache.maven.api.ProjectScope; +import org.apache.maven.api.Session; +import org.apache.maven.api.di.Inject; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; +import org.apache.maven.api.services.ProjectManager; /** * Add test source directories to the POM. @@ -38,7 +41,7 @@ * @author Dan T. Tran * @since 1.0 */ -@Mojo(name = "add-test-source", defaultPhase = LifecyclePhase.GENERATE_TEST_SOURCES, threadSafe = true) +@Mojo(name = "add-test-source", defaultPhase = "generate-test-sources") public class AddTestSourceMojo extends AbstractMojo { /** @@ -47,13 +50,7 @@ public class AddTestSourceMojo extends AbstractMojo { * @since 1.0 */ @Parameter(required = true) - private File[] sources; - - /** - * @since 1.0 - */ - @Parameter(readonly = true, defaultValue = "${project}") - private MavenProject project; + private Path[] sources; /** * Skip plugin execution. @@ -71,6 +68,12 @@ public class AddTestSourceMojo extends AbstractMojo { @Parameter(property = "buildhelper.addtestsource.skipIfMissing", defaultValue = "false") private boolean skipAddTestSourceIfMissing; + @Inject + private Project project; + + @Inject + private Session session; + public void execute() { if (skipAddTestSource) { if (getLog().isInfoEnabled()) { @@ -79,13 +82,14 @@ public void execute() { return; } - for (File source : sources) { - if (skipAddTestSourceIfMissing && !source.exists()) { + for (Path source : sources) { + if (skipAddTestSourceIfMissing && !Files.exists(source)) { if (getLog().isDebugEnabled()) { getLog().debug("Skipping directory: " + source + ", because it does not exist."); } } else { - this.project.addTestCompileSourceRoot(source.getAbsolutePath()); + ProjectManager projectManager = session.getService(ProjectManager.class); + projectManager.addCompileSourceRoot(project, ProjectScope.TEST, source.toAbsolutePath()); if (getLog().isInfoEnabled()) { getLog().info("Test Source directory: " + source + " added."); } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/AttachArtifactMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/AttachArtifactMojo.java index e51c93fc..0f38e492 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/AttachArtifactMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/AttachArtifactMojo.java @@ -27,15 +27,15 @@ import java.util.HashSet; import java.util.Set; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.Component; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.project.MavenProject; -import org.apache.maven.project.MavenProjectHelper; +import org.apache.maven.api.Project; +import org.apache.maven.api.Session; +import org.apache.maven.api.di.Inject; +import org.apache.maven.api.plugin.MojoException; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; +import org.apache.maven.api.services.ArtifactFactory; +import org.apache.maven.api.services.ProjectManager; +import org.codehaus.mojo.buildhelper.utils.Artifact; /** * Attach additional artifacts to be installed and deployed. @@ -43,7 +43,7 @@ * @author Dan T. Tran * @since 1.0 */ -@Mojo(name = "attach-artifact", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true) +@Mojo(name = "attach-artifact", defaultPhase = "package") public class AttachArtifactMojo extends AbstractMojo { /** * Attach an array of artifacts to the project. @@ -51,15 +51,15 @@ public class AttachArtifactMojo extends AbstractMojo { @Parameter(required = true) private Artifact[] artifacts; - @Parameter(readonly = true, defaultValue = "${project}") - private MavenProject project; + @Inject + private Project project; + + @Inject + private Session session; /** * Maven ProjectHelper. */ - @Component - private MavenProjectHelper projectHelper; - /** * This will cause the execution to be run only at the top of a given module tree. That is, run in the project * contained in the same folder where the mvn execution was launched. @@ -78,34 +78,45 @@ public class AttachArtifactMojo extends AbstractMojo { @Parameter(property = "buildhelper.skipAttach", defaultValue = "false") private boolean skipAttach; - public void execute() throws MojoExecutionException, MojoFailureException { - + public void execute() throws MojoException, MojoException { if (skipAttach) { getLog().info("Skip attaching artifacts"); return; } // Run only at the execution root - if (runOnlyAtExecutionRoot && !project.isExecutionRoot()) { + if (runOnlyAtExecutionRoot && !project.isRootProject()) { getLog().info("Skip attaching artifacts in this project because it's not the Execution Root"); } else { this.validateArtifacts(); for (Artifact artifact : artifacts) { - projectHelper.attachArtifact( - this.project, artifact.getType(), artifact.getClassifier(), artifact.getFile()); + + ProjectManager projectManager = session.getService(ProjectManager.class); + projectManager.attachArtifact( + project, + session.getService(ArtifactFactory.class) + .create( + session, + project.getGroupId(), + project.getArtifactId(), + project.getVersion(), + artifact.getClassifier(), + null, + artifact.getType()), + artifact.getPath()); } } } - private void validateArtifacts() throws MojoFailureException { + private void validateArtifacts() throws MojoException { // check unique of types and classifiers Set extensionClassifiers = new HashSet(); for (Artifact artifact : artifacts) { String extensionClassifier = artifact.getType() + ":" + artifact.getClassifier(); if (!extensionClassifiers.add(extensionClassifier)) { - throw new MojoFailureException("The artifact with same type and classifier: " + extensionClassifier + throw new MojoException("The artifact with same type and classifier: " + extensionClassifier + " is used more than once."); } } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/BeanshellPropertyMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/BeanshellPropertyMojo.java index 3f6524ff..2a0c6066 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/BeanshellPropertyMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/BeanshellPropertyMojo.java @@ -26,12 +26,12 @@ import bsh.EvalError; import bsh.Interpreter; -import org.apache.maven.execution.MavenSession; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.settings.Settings; +import org.apache.maven.api.Session; +import org.apache.maven.api.di.Inject; +import org.apache.maven.api.plugin.MojoException; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; +import org.apache.maven.api.settings.Settings; /** * Define one or many properties as a result of a Beanshell script invocation. Like @@ -39,15 +39,15 @@ * defined: * * * @author Hervé Boutemy * @since 1.8 */ -@Mojo(name = "bsh-property", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true) +@Mojo(name = "bsh-property", defaultPhase = "validate") public class BeanshellPropertyMojo extends AbstractDefinePropertyMojo { @Parameter(required = true) private String source; @@ -62,31 +62,30 @@ public class BeanshellPropertyMojo extends AbstractDefinePropertyMojo { /** * The Maven Session. */ - @Parameter(readonly = true, defaultValue = "${session}") - private MavenSession mavenSession; + @Inject + private Session session; /** * The Maven Settings. */ - @Parameter(readonly = true, defaultValue = "${settings}") + @Parameter(readonly = true, defaultValue = "${session.settings}") private Settings settings; /** * Main plugin execution */ - public void execute() throws MojoFailureException { + public void execute() throws MojoException { Interpreter interpreter = new Interpreter(); set(interpreter, "project", getProject()); - set(interpreter, "session", mavenSession); + set(interpreter, "session", session); set(interpreter, "settings", settings); set(interpreter, "log", getLog()); try { interpreter.eval(source); } catch (EvalError ee) { - MojoFailureException mfe = - new MojoFailureException("error during Beanshell script execution: " + ee.getMessage()); + MojoException mfe = new MojoException("error during Beanshell script execution: " + ee.getMessage()); mfe.initCause(ee); throw mfe; } @@ -101,7 +100,7 @@ public void execute() throws MojoFailureException { defineProperty(property, value.toString()); } } catch (EvalError ee) { - MojoFailureException mfe = new MojoFailureException( + MojoException mfe = new MojoException( "cannot get Beanshell global variable '" + property + "': " + ee.getMessage()); mfe.initCause(ee); throw mfe; @@ -110,12 +109,12 @@ public void execute() throws MojoFailureException { } } - private void set(Interpreter interpreter, String name, Object value) throws MojoFailureException { + private void set(Interpreter interpreter, String name, Object value) throws MojoException { try { interpreter.set(name, value); } catch (EvalError ee) { - MojoFailureException mfe = new MojoFailureException( - "cannot define Beanshell global variable '" + name + "': " + ee.getMessage()); + MojoException mfe = + new MojoException("cannot define Beanshell global variable '" + name + "': " + ee.getMessage()); mfe.initCause(ee); throw mfe; } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/CpuCountMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/CpuCountMojo.java index 6602a146..2548ca69 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/CpuCountMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/CpuCountMojo.java @@ -24,10 +24,9 @@ * SOFTWARE. */ -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.api.plugin.MojoException; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; /** * Retrieve number of CPUs with project factor, and place it under a configurable project property @@ -35,7 +34,7 @@ * @author Dan T. Tran * @since 1.9 */ -@Mojo(name = "cpu-count", defaultPhase = LifecyclePhase.INITIALIZE, threadSafe = true) +@Mojo(name = "cpu-count", defaultPhase = "initialize") public class CpuCountMojo extends AbstractDefinePropertyMojo { /** @@ -50,13 +49,15 @@ public class CpuCountMojo extends AbstractDefinePropertyMojo { @Parameter(defaultValue = "1.0") private float factor; - public void execute() throws MojoExecutionException { + public void execute() throws MojoException { float count = Runtime.getRuntime().availableProcessors() * factor; if (count < 1) { count = 1; } - defineProperty(this.cpuCount, Integer.toString((int) count)); - this.getLog().info("CPU count: " + this.getProject().getProperties().getProperty(cpuCount)); + defineProperty(cpuCount, Integer.toString((int) count)); + this.getLog() + .info("CPU count: " + + this.getProject().getModel().getProperties().get(cpuCount)); } } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/HostnameMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/HostnameMojo.java index 4fd33cd9..ea3e682e 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/HostnameMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/HostnameMojo.java @@ -27,10 +27,9 @@ import java.net.InetAddress; import java.net.UnknownHostException; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.api.plugin.MojoException; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; /** * Retrieve current hostname and place it under a configurable project property @@ -38,7 +37,7 @@ * @author John Patrick * @since 3.0.0 */ -@Mojo(name = "hostname", defaultPhase = LifecyclePhase.PROCESS_TEST_CLASSES, threadSafe = true) +@Mojo(name = "hostname", defaultPhase = "process-test-classes") public class HostnameMojo extends AbstractDefinePropertyMojo { /** @@ -47,11 +46,11 @@ public class HostnameMojo extends AbstractDefinePropertyMojo { @Parameter(defaultValue = "hostname") private String hostnameProperty; - public void execute() throws MojoExecutionException { + public void execute() throws MojoException { try { defineProperty(this.hostnameProperty, InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { - throw new MojoExecutionException("Unable to retrieve hostname.", e); + throw new MojoException("Unable to retrieve hostname.", e); } } } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/LocalIpMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/LocalIpMojo.java index 29ef4d31..a979ffa3 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/LocalIpMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/LocalIpMojo.java @@ -27,10 +27,9 @@ import java.net.InetAddress; import java.net.UnknownHostException; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.api.plugin.MojoException; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; /** * Retrieve current host IP address and place it under a configurable project property @@ -38,7 +37,7 @@ * @author Dan T. Tran * @since 1.8 */ -@Mojo(name = "local-ip", defaultPhase = LifecyclePhase.PROCESS_TEST_CLASSES, threadSafe = true) +@Mojo(name = "local-ip", defaultPhase = "process-test-classes") public class LocalIpMojo extends AbstractDefinePropertyMojo { /** @@ -47,11 +46,11 @@ public class LocalIpMojo extends AbstractDefinePropertyMojo { @Parameter(defaultValue = "local.ip") private String localIpProperty; - public void execute() throws MojoExecutionException { + public void execute() throws MojoException { try { defineProperty(this.localIpProperty, InetAddress.getLocalHost().getHostAddress()); } catch (UnknownHostException e) { - throw new MojoExecutionException("Unable to retrieve localhost address.", e); + throw new MojoException("Unable to retrieve localhost address.", e); } } } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/MavenVersionMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/MavenVersionMojo.java deleted file mode 100644 index 66b9154c..00000000 --- a/src/main/java/org/codehaus/mojo/buildhelper/MavenVersionMojo.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.codehaus.mojo.buildhelper; - -/* - * The MIT License - * - * Copyright (c) 2004, The Codehaus - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is furnished to do - * so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -import org.apache.maven.plugins.annotations.Component; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.rtinfo.RuntimeInformation; - -/** - * Store the maven core version in a property maven.version. - * - * @author pgier - * @since 1.3 - * @deprecated Maven since version {@code 3.0.4} has such property build in: - * MNG-4112. - * So goal can be removed. - */ -@Deprecated -@Mojo(name = "maven-version", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true) -public class MavenVersionMojo extends AbstractDefinePropertyMojo { - - /** - * The RuntimeInforamtion for the current instance of Maven. - */ - @Component - private RuntimeInformation runtime; - - /** - * The name of the property in which to store the version of Maven. - */ - @Parameter(defaultValue = "maven.version") - private String versionProperty; - - /** - * Main plugin execution - */ - public void execute() { - defineProperty(versionProperty, runtime.getMavenVersion()); - } -} diff --git a/src/main/java/org/codehaus/mojo/buildhelper/ParseVersionMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/ParseVersionMojo.java index fe2ab763..53b66b18 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/ParseVersionMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/ParseVersionMojo.java @@ -1,9 +1,8 @@ package org.codehaus.mojo.buildhelper; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.codehaus.mojo.buildhelper.versioning.DefaultVersioning; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; +import org.codehaus.mojo.buildhelper.utils.ParsedVersion; /** * Parse a version string and set properties containing the component parts of the version. This mojo sets the following @@ -89,7 +88,7 @@ * @author pgier * @since 1.3 */ -@Mojo(name = "parse-version", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true) +@Mojo(name = "parse-version", defaultPhase = "validate") public class ParseVersionMojo extends AbstractDefinePropertyMojo { /** @@ -178,7 +177,7 @@ private void defineVersionProperty(String name, long value) { * @param version the version to parse */ public void parseVersion(String version) { - DefaultVersioning artifactVersion = new DefaultVersioning(version); + ParsedVersion artifactVersion = new ParsedVersion(version); getLog().debug("Parsed Version"); getLog().debug(" major: " + artifactVersion.getMajor()); diff --git a/src/main/java/org/codehaus/mojo/buildhelper/RegexPropertiesMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/RegexPropertiesMojo.java index ce44341f..bed715ae 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/RegexPropertiesMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/RegexPropertiesMojo.java @@ -27,11 +27,9 @@ import java.util.ArrayList; import java.util.List; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.api.plugin.MojoException; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; /** * Sets a property by applying a regex replacement rule to a supplied value. This is similar to regex-property goal with @@ -39,7 +37,7 @@ * * @since 1.9 */ -@Mojo(name = "regex-properties", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true) +@Mojo(name = "regex-properties", defaultPhase = "validate") public class RegexPropertiesMojo extends AbstractRegexPropertyMojo { /** * List of RegexPropertyConfig to apply the regex @@ -47,8 +45,7 @@ public class RegexPropertiesMojo extends AbstractRegexPropertyMojo { @Parameter(required = false) private List regexPropertySettings = new ArrayList(); - public void execute() throws MojoExecutionException, MojoFailureException { - + public void execute() throws MojoException { for (RegexPropertySetting setting : regexPropertySettings) { this.execute(setting); } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/RegexPropertyMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/RegexPropertyMojo.java index a6f75fb7..04eca407 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/RegexPropertyMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/RegexPropertyMojo.java @@ -24,11 +24,9 @@ * SOFTWARE. */ -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.api.plugin.MojoException; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; /** * Sets a property by applying a regex replacement rule to a supplied value. @@ -36,7 +34,7 @@ * @author Stephen Connolly * @since 1.7 */ -@Mojo(name = "regex-property", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true) +@Mojo(name = "regex-property", defaultPhase = "validate") public class RegexPropertyMojo extends AbstractRegexPropertyMojo { /** * The property to set. @@ -87,7 +85,7 @@ public class RegexPropertyMojo extends AbstractRegexPropertyMojo { /** * {@inheritDoc} */ - public void execute() throws MojoExecutionException, MojoFailureException { + public void execute() throws MojoException { RegexPropertySetting config = new RegexPropertySetting(); config.setName(name); config.setValue(value); diff --git a/src/main/java/org/codehaus/mojo/buildhelper/RegexPropertySetting.java b/src/main/java/org/codehaus/mojo/buildhelper/RegexPropertySetting.java index 3b27a558..cb98b211 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/RegexPropertySetting.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/RegexPropertySetting.java @@ -24,7 +24,7 @@ * SOFTWARE. */ -import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.api.plugin.annotations.Parameter; import org.codehaus.plexus.util.StringUtils; public class RegexPropertySetting { diff --git a/src/main/java/org/codehaus/mojo/buildhelper/ReleasedVersionMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/ReleasedVersionMojo.java index f166eb61..e09c86de 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/ReleasedVersionMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/ReleasedVersionMojo.java @@ -25,20 +25,18 @@ */ import java.util.Objects; - -import org.apache.maven.artifact.ArtifactUtils; -import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; -import org.apache.maven.artifact.versioning.DefaultArtifactVersion; -import org.apache.maven.plugins.annotations.Component; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.eclipse.aether.RepositorySystem; -import org.eclipse.aether.RepositorySystemSession; -import org.eclipse.aether.artifact.DefaultArtifact; -import org.eclipse.aether.resolution.VersionRangeRequest; -import org.eclipse.aether.resolution.VersionRangeResolutionException; -import org.eclipse.aether.resolution.VersionRangeResult; +import java.util.stream.Stream; + +import org.apache.maven.api.ArtifactCoordinate; +import org.apache.maven.api.Session; +import org.apache.maven.api.Version; +import org.apache.maven.api.di.Inject; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; +import org.apache.maven.api.services.VersionRangeResolver; +import org.apache.maven.api.services.VersionRangeResolverException; +import org.apache.maven.api.services.VersionRangeResolverResult; +import org.codehaus.mojo.buildhelper.utils.ParsedVersion; /** * Resolve the latest released version of this project. This mojo sets the following properties: @@ -57,20 +55,11 @@ * @author Robert Scholte * @since 1.6 */ -@Mojo(name = "released-version", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true) +@Mojo(name = "released-version", defaultPhase = "validate") public class ReleasedVersionMojo extends AbstractDefinePropertyMojo { - /** - * The artifact metadata source to use. - */ - @Component - private RepositorySystem repoSystem; - - @Component - private ArtifactHandlerManager artifactHandlerManager; - - @Parameter(defaultValue = "${repositorySystemSession}", readonly = true) - private RepositorySystemSession repoSession; + @Inject + private Session session; /** * Prefix string to use for the set of version properties. @@ -82,8 +71,8 @@ private void defineVersionProperty(String name, String value) { defineProperty(propertyPrefix + '.' + name, Objects.toString(value, "")); } - private void defineVersionProperty(String name, int value) { - defineVersionProperty(name, Integer.toString(value)); + private void defineVersionProperty(String name, long value) { + defineVersionProperty(name, Long.toString(value)); } @SuppressWarnings("unchecked") @@ -99,26 +88,28 @@ public void execute() { */ try { - DefaultArtifact artifact = new DefaultArtifact( + VersionRangeResolver resolver = session.getService(VersionRangeResolver.class); + + ArtifactCoordinate artifact = session.createArtifactCoordinate( getProject().getGroupId(), getProject().getArtifactId(), - artifactHandlerManager - .getArtifactHandler(getProject().getPackaging()) - .getExtension(), - "[0,)"); + "[0,)", + getProject().getPackaging().type().getExtension()); getLog().debug("Artifact for lookup released version: " + artifact); - VersionRangeRequest request = - new VersionRangeRequest(artifact, getProject().getRemoteProjectRepositories(), null); - VersionRangeResult versionRangeResult = repoSystem.resolveVersionRange(repoSession, request); + Session s = session.withRemoteRepositories(Stream.concat( + session.getRemoteRepositories().stream(), + project.getModel().getRepositories().stream().map(session::createRemoteRepository)) + .toList()); + VersionRangeResolverResult versionRangeResult = resolver.resolve(s, artifact); getLog().debug("Resolved versions: " + versionRangeResult.getVersions()); - DefaultArtifactVersion releasedVersion = versionRangeResult.getVersions().stream() - .filter(v -> !ArtifactUtils.isSnapshot(v.toString())) - .map(v -> new DefaultArtifactVersion(v.toString())) - .max(DefaultArtifactVersion::compareTo) + ParsedVersion releasedVersion = versionRangeResult.getVersions().stream() + .filter(v -> !session.isVersionSnapshot(v.toString())) + .max(Version::compareTo) + .map(v -> new ParsedVersion(v.asString())) .orElse(null); getLog().debug("Released version: " + releasedVersion); @@ -134,16 +125,16 @@ public void execute() { } defineVersionProperty("version", releasedVersionValue); - defineVersionProperty("majorVersion", releasedVersion.getMajorVersion()); - defineVersionProperty("minorVersion", releasedVersion.getMinorVersion()); - defineVersionProperty("incrementalVersion", releasedVersion.getIncrementalVersion()); + defineVersionProperty("majorVersion", releasedVersion.getMajor()); + defineVersionProperty("minorVersion", releasedVersion.getMinor()); + defineVersionProperty("incrementalVersion", releasedVersion.getPatch()); defineVersionProperty("buildNumber", releasedVersion.getBuildNumber()); defineVersionProperty("qualifier", releasedVersion.getQualifier()); } else { getLog().debug("No released version found."); } - } catch (VersionRangeResolutionException e) { + } catch (VersionRangeResolverException e) { if (getLog().isWarnEnabled()) { getLog().warn("Failed to retrieve artifacts metadata, cannot resolve the released version"); } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/RemoveLocalArtifactMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/RemoveLocalArtifactMojo.java deleted file mode 100644 index 311c6c2d..00000000 --- a/src/main/java/org/codehaus/mojo/buildhelper/RemoveLocalArtifactMojo.java +++ /dev/null @@ -1,109 +0,0 @@ -package org.codehaus.mojo.buildhelper; - -/* - * The MIT License - * - * Copyright (c) 2004, The Codehaus - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is furnished to do - * so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -import java.io.File; -import java.io.IOException; - -import org.apache.maven.RepositoryUtils; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.project.MavenProject; -import org.codehaus.plexus.util.FileUtils; -import org.eclipse.aether.RepositorySystemSession; -import org.eclipse.aether.repository.LocalRepositoryManager; - -/** - * Remove project's artifacts from local repository. Useful to keep only one copy of large local snapshot, for example: - * installer, for disk space optimization purpose. - * - * @author Dan T. Tran - * @since 1.1 - * @deprecated There is a similar goal: - * dependency:purge-local-repository - * By the way such goal should by not used with Maven 3/4 anymore. - */ -@Deprecated -@Mojo(name = "remove-project-artifact", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true) -public class RemoveLocalArtifactMojo extends AbstractMojo { - - /** - * When true, remove all built artifacts including all versions. When false, remove all built artifacts of this - * project version. - * - * @since 1.1 - */ - @Parameter(defaultValue = "true", property = "buildhelper.removeAll") - private boolean removeAll; - - /** - * Indicates whether the build will continue even if there are removal errors. - * - * @since 1.6 - */ - @Parameter(defaultValue = "true", property = "buildhelper.failOnError") - private boolean failOnError; - - /** - * @since 1.1 - */ - @Parameter(readonly = true, defaultValue = "${project}") - private MavenProject project; - - @Parameter(readonly = true, defaultValue = "${repositorySystemSession}") - private RepositorySystemSession repoSession; - - public void execute() throws MojoExecutionException { - LocalRepositoryManager lrm = repoSession.getLocalRepositoryManager(); - - String artifactPath = lrm.getPathForLocalArtifact(RepositoryUtils.toArtifact(project.getArtifact())); - File repoBasedir = lrm.getRepository().getBasedir(); - - File localArtifactFile = new File(repoBasedir, artifactPath); - File localArtifactDirectory = localArtifactFile.getParentFile(); - - if (removeAll) { - localArtifactDirectory = localArtifactDirectory.getParentFile(); - } - - try { - FileUtils.deleteDirectory(localArtifactDirectory); - - if (getLog().isInfoEnabled()) { - getLog().info(localArtifactDirectory.getAbsolutePath() + " removed."); - } - } catch (IOException e) { - final String failureMessage = "Cannot delete " + localArtifactDirectory; - if (failOnError) { - throw new MojoExecutionException(failureMessage); - } else { - getLog().warn(failureMessage); - } - } - } -} diff --git a/src/main/java/org/codehaus/mojo/buildhelper/ReserveListenerPortMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/ReserveListenerPortMojo.java index 28070c0d..c7f3f70b 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/ReserveListenerPortMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/ReserveListenerPortMojo.java @@ -26,8 +26,6 @@ import java.io.BufferedInputStream; import java.io.BufferedReader; -import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -35,28 +33,30 @@ import java.net.MalformedURLException; import java.net.ServerSocket; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Properties; -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.project.MavenProject; -import org.codehaus.plexus.util.IOUtil; +import org.apache.maven.api.Project; +import org.apache.maven.api.Session; +import org.apache.maven.api.di.Inject; +import org.apache.maven.api.plugin.MojoException; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; +import org.apache.maven.api.services.ProjectManager; +import org.codehaus.plexus.util.io.CachingOutputStream; /** * Reserve a list of random and not in use network ports and place them in a configurable project properties. * - * @author Dan T. Tran - * @version $Id: ReserveListnerPortMojo.java 6754 2008-04-13 15:14:04Z dantran $ * @since 1.2 */ -@Mojo(name = "reserve-network-port", defaultPhase = LifecyclePhase.PROCESS_TEST_CLASSES, threadSafe = true) +@Mojo(name = "reserve-network-port", defaultPhase = "process-test-classes") public class ReserveListenerPortMojo extends AbstractMojo { private static final String BUILD_HELPER_RESERVED_PORTS = "BUILD_HELPER_MIN_PORT"; @@ -67,13 +67,13 @@ public class ReserveListenerPortMojo extends AbstractMojo { private static final Object LOCK = new Object(); /** - * A List to property names to be placed in Maven project. At least one of {@code #urls} or {@code #portNames} has + * A List of property names to be placed in Maven project. At least one of {@code #urls} or {@code #portNames} has * to be specified. * * @since 1.2 */ @Parameter - private String[] portNames = new String[0]; + private List portNames = List.of(); /** * A List of urls to resource where list of name could be found. Can be in form of classpath:com/myproject/names.txt @@ -82,7 +82,7 @@ public class ReserveListenerPortMojo extends AbstractMojo { * @since 1.11 */ @Parameter - private String[] urls = new String[0]; + private List urls = List.of(); /** * Output file to write the generated properties to. if not given, they are written to Maven project @@ -90,7 +90,7 @@ public class ReserveListenerPortMojo extends AbstractMojo { * @since 1.2 */ @Parameter - private File outputFile; + private Path outputFile; /** * Specify this if you want the port be chosen with a number higher than that one. @@ -119,19 +119,15 @@ public class ReserveListenerPortMojo extends AbstractMojo { @Parameter private boolean randomPort; - /** - * @since 1.2 - */ - @Parameter(readonly = true, defaultValue = "${project}") - private MavenProject project; + @Inject + private Session session; - @Override - public void execute() throws MojoExecutionException { - Properties properties = project.getProperties(); + @Inject + private Project project; - if (outputFile != null) { - properties = new Properties(); - } + @Override + public void execute() throws MojoException { + Properties properties = new Properties(); loadUrls(); @@ -142,32 +138,24 @@ public void execute() throws MojoExecutionException { try { final ServerSocket socket = getServerSocket(); sockets.add(socket); - final String unusedPort = Integer.toString(socket.getLocalPort()); + ProjectManager projectManager = session.getService(ProjectManager.class); + projectManager.setProperty(project, portName, unusedPort); properties.put(portName, unusedPort); getReservedPorts().add(socket.getLocalPort()); this.getLog().info("Reserved port " + unusedPort + " for " + portName); } catch (IOException e) { - throw new MojoExecutionException("Error getting an available port from system", e); + throw new MojoException("Error getting an available port from system", e); } } // Write the file -- still hold onto the ports if (outputFile != null) { - try { - createOutputDirectoryIfNotExist(outputFile); - } catch (IOException e) { - throw new MojoExecutionException(e.getMessage()); - } - - OutputStream os = null; - try { - os = new FileOutputStream(outputFile); + createOutputDirectoryIfNotExist(outputFile); + try (OutputStream os = new CachingOutputStream(outputFile)) { properties.store(os, null); } catch (Exception e) { - throw new MojoExecutionException(e.getMessage()); - } finally { - IOUtil.close(os); + throw new MojoException(e); } } } finally { @@ -183,18 +171,28 @@ public void execute() throws MojoExecutionException { } } - private void createOutputDirectoryIfNotExist(File outputFile) throws IOException { - File parentDirectory = new File(outputFile.getCanonicalFile().getParent()); + private void createOutputDirectoryIfNotExist(Path outputFile) { + Path parentDirectory = getCanonicalPath(outputFile.toAbsolutePath()).getParent(); - if (!parentDirectory.exists()) { - getLog().debug("Trying to create directories: " + parentDirectory.getAbsolutePath()); - if (!parentDirectory.mkdirs()) { - getLog().error("Failed to create folders " + parentDirectory.getAbsolutePath()); + if (!Files.exists(parentDirectory)) { + getLog().debug("Trying to create directories: " + parentDirectory); + try { + Files.createDirectories(parentDirectory); + } catch (IOException e) { + getLog().error("Failed to create folders " + parentDirectory); } } } - private ServerSocket getServerSocket() throws IOException, MojoExecutionException { + private static Path getCanonicalPath(Path path) { + try { + return path.toRealPath(); + } catch (IOException e) { + return getCanonicalPath(path.getParent()).resolve(path.getFileName()); + } + } + + private ServerSocket getServerSocket() throws IOException, MojoException { if (minPortNumber == null && maxPortNumber != null) { getLog().debug("minPortNumber unspecified: using default value " + FIRST_NON_ROOT_PORT_NUMBER); minPortNumber = FIRST_NON_ROOT_PORT_NUMBER; @@ -217,7 +215,7 @@ private ServerSocket getServerSocket() throws IOException, MojoExecutionExceptio return serverSocket; } } - throw new MojoExecutionException( + throw new MojoException( "Unable to find an available port between " + minPortNumber + " and " + maxPortNumber); } } else { @@ -225,9 +223,7 @@ private ServerSocket getServerSocket() throws IOException, MojoExecutionExceptio // threading issues (essentially possible while put/getting the plugin ctx to get the reserved ports). synchronized (LOCK) { int min = getNextPortNumber(); - for (int port = min; ; ++port) { - ServerSocket serverSocket = reservePort(port); if (serverSocket != null) { return serverSocket; @@ -238,7 +234,6 @@ private ServerSocket getServerSocket() throws IOException, MojoExecutionExceptio } private List randomPortList() { - int difference = maxPortNumber - minPortNumber + 1; List portList = new ArrayList(difference); List reservedPorts = getReservedPorts(); @@ -252,10 +247,9 @@ private List randomPortList() { return portList; } - public ServerSocket reservePort(int port) throws MojoExecutionException { - + public ServerSocket reservePort(int port) throws MojoException { if (port > maxPortNumber) { - throw new MojoExecutionException( + throw new MojoException( "Unable to find an available port between " + minPortNumber + " and " + maxPortNumber); } try { @@ -285,11 +279,11 @@ private int getNextPortNumber() { @SuppressWarnings("unchecked") private List getReservedPorts() { - - List reservedPorts = (List) getPluginContext().get(BUILD_HELPER_RESERVED_PORTS); + Map pluginContext = session.getPluginContext(project); + List reservedPorts = (List) pluginContext.get(BUILD_HELPER_RESERVED_PORTS); if (reservedPorts == null) { - reservedPorts = new ArrayList(); - getPluginContext().put(BUILD_HELPER_RESERVED_PORTS, reservedPorts); + reservedPorts = new ArrayList<>(); + pluginContext.put(BUILD_HELPER_RESERVED_PORTS, reservedPorts); } return reservedPorts; } @@ -311,50 +305,38 @@ private int findAvailablePortNumber(Integer portNumberStartingPoint, List names = new ArrayList<>(this.portNames); for (String url : urls) { - load(new UrlResource(url)); + names.addAll(load(new UrlResource(url))); } + this.portNames = names; } - private void load(UrlResource resource) throws MojoExecutionException { + private List load(UrlResource resource) throws MojoException { if (resource.canBeOpened()) { - loadPortNamesFromResource(resource); + return loadPortNamesFromResource(resource); } else { - throw new MojoExecutionException("Port names could not be loaded from \"" + resource + "\""); + throw new MojoException("Port names could not be loaded from \"" + resource + "\""); } } - private void loadPortNamesFromResource(UrlResource resource) throws MojoExecutionException { - try { + private List loadPortNamesFromResource(UrlResource resource) throws MojoException { + if (getLog().isDebugEnabled()) { + getLog().debug("Loading port names from " + resource); + } + try (InputStream stream = resource.getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) { + List names = reader.lines() + .map(String::trim) + .filter(line -> !line.isEmpty() && !line.startsWith("#")) + .toList(); if (getLog().isDebugEnabled()) { - getLog().debug("Loading port names from " + resource); - } - final InputStream stream = resource.getInputStream(); - - try { - BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); - List names = new ArrayList(); - String line; - while ((line = reader.readLine()) != null) { - line = line.trim(); - if (!line.isEmpty() && !line.replace(" ", "").startsWith("#")) { - names.add(line); - } - } - if (getLog().isDebugEnabled()) { - getLog().debug("Loaded port names " + names); - } - String[] tPortNames = names.toArray(new String[portNames.length + names.size()]); - if (portNames.length > 0) { - System.arraycopy(portNames, 0, tPortNames, names.size(), portNames.length); - } - portNames = tPortNames; - } finally { - stream.close(); + getLog().debug("Loaded port names " + names); } + return names; } catch (IOException e) { - throw new MojoExecutionException("Error reading port names from \"" + resource + "\"", e); + throw new MojoException("Error reading port names from \"" + resource + "\"", e); } } @@ -371,11 +353,11 @@ private class UrlResource { private InputStream stream; - UrlResource(String url) throws MojoExecutionException { + UrlResource(String url) throws MojoException { if (url.startsWith(CLASSPATH_PREFIX)) { - String resource = url.substring(CLASSPATH_PREFIX.length(), url.length()); + String resource = url.substring(CLASSPATH_PREFIX.length()); if (resource.startsWith(SLASH_PREFIX)) { - resource = resource.substring(1, resource.length()); + resource = resource.substring(1); } this.url = getClass().getClassLoader().getResource(resource); if (this.url == null) { @@ -389,7 +371,7 @@ private class UrlResource { try { this.url = new URL(url); } catch (MalformedURLException e) { - throw new MojoExecutionException("Badly formed URL " + url + " - " + e.getMessage()); + throw new MojoException("Badly formed URL " + url + " - " + e.getMessage()); } } } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/RootLocationMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/RootLocationMojo.java deleted file mode 100644 index 53811a8b..00000000 --- a/src/main/java/org/codehaus/mojo/buildhelper/RootLocationMojo.java +++ /dev/null @@ -1,140 +0,0 @@ -package org.codehaus.mojo.buildhelper; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; -import java.util.TreeSet; - -import org.apache.maven.execution.MavenSession; -import org.apache.maven.model.Model; -import org.apache.maven.model.Profile; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.Component; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.project.MavenProject; -import org.apache.maven.project.ProjectBuilder; -import org.apache.maven.project.ProjectBuildingException; -import org.apache.maven.project.ProjectBuildingResult; - -/** - * This goal will get the location of the root folder within a multi module build as a property {@code rootlocation}. - * - * @author Karl Heinz Marbaise - * @since 3.0.0 - */ -@Mojo(name = "rootlocation", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true, requiresProject = true) -public class RootLocationMojo extends AbstractDefinePropertyMojo { - - @Parameter(defaultValue = "${session}", readonly = true, required = true) - private MavenSession session; - - @Component - private ProjectBuilder projectBuilder; - - /** - * This will cause the execution to be run only at the top of a given module tree. - */ - @Parameter(property = "buildhelper.runOnlyAtExecutionRoot", defaultValue = "false") - private boolean runOnlyAtExecutionRoot; - - /** - * The name of the property in which to store the root location. - */ - @Parameter(defaultValue = "rootlocation") - private String rootLocationProperty; - - /** - * Main plugin execution - */ - public void execute() throws MojoFailureException { - if (runOnlyAtExecutionRoot && !getProject().isExecutionRoot()) { - getLog().info("Skip getting the rootlocation in this project because it's not the Execution Root"); - } else { - try { - MavenProject topLevelProject = getLocalRoot(project); - defineProperty( - rootLocationProperty, topLevelProject.getBasedir().getAbsolutePath()); - } catch (IOException ex) { - throw new MojoFailureException("Unable to detect root location: " + ex.getMessage(), ex); - } - } - } - - /** - * Finds the local root of the specified project. - * - * @param project The project to find the local root for. - * @return The local root project (this may be the current project) - */ - private MavenProject getLocalRoot(final MavenProject project) throws IOException { - MavenProject currentProject = project; - MavenProject localRootProject = project; - - List parentDirs = new ArrayList<>(); - getAllParentDirectories(project.getBasedir(), parentDirs); - - for (File parentDir : parentDirs) { - getLog().debug("Checking to see if " + parentDir + " is an aggregator parent"); - File parent = new File(parentDir, "pom.xml"); - if (parent.isFile()) { - try { - final ProjectBuildingResult result = - projectBuilder.build(parent, session.getProjectBuildingRequest()); - final MavenProject parentProject = result.getProject(); - final String currentProjectCanonicalPath = - currentProject.getBasedir().getCanonicalPath(); - if (getAllChildModules(parentProject).contains(currentProjectCanonicalPath)) { - getLog().debug(parentDir + " is an aggregator parent of current project "); - localRootProject = parentProject; - currentProject = parentProject; - } else { - getLog().debug(parentDir + " is not an aggregator parent of current project (" - + getAllChildModules(parentProject) + "/" + currentProjectCanonicalPath + ") "); - } - } catch (ProjectBuildingException e) { - getLog().warn(e); - } - } - } - - getLog().debug("Local aggregation root is " + localRootProject.getBasedir()); - return localRootProject; - } - - private void getAllParentDirectories(File directory, List parents) { - File parent = directory.getParentFile(); - if (parent != null && parent.isDirectory()) { - parents.add(parent); - getAllParentDirectories(parent, parents); - } - } - - /** - * Returns a set of all child modules for a project, including any defined in profiles (ignoring profile activation). - * - * @param project The project. - * @return the set of all child modules of the project (canonical paths). - */ - private Set getAllChildModules(MavenProject project) throws IOException { - Model model = project.getOriginalModel(); - Set paths = new TreeSet<>(); - paths.addAll(getChildModuleCanoncialPath(project, model.getModules())); - for (Profile profile : model.getProfiles()) { - paths.addAll(getChildModuleCanoncialPath(project, profile.getModules())); - } - return paths; - } - - private Set getChildModuleCanoncialPath(MavenProject project, List modules) throws IOException { - Set paths = new TreeSet<>(); - for (String module : modules) { - File file = new File(project.getBasedir(), module); - paths.add(file.getCanonicalPath()); - } - return paths; - } -} diff --git a/src/main/java/org/codehaus/mojo/buildhelper/TimestampPropertyMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/TimestampPropertyMojo.java index fdefc479..e931169d 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/TimestampPropertyMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/TimestampPropertyMojo.java @@ -32,12 +32,11 @@ import java.util.Locale; import java.util.TimeZone; -import org.apache.maven.execution.MavenSession; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.LifecyclePhase; -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.MojoException; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; /** * Sets a property based on the current date and time. @@ -45,7 +44,7 @@ * @author Stephen Connolly * @since 1.7 */ -@Mojo(name = "timestamp-property", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true) +@Mojo(name = "timestamp-property", defaultPhase = "validate") public class TimestampPropertyMojo extends AbstractDefinePropertyMojo { /** @@ -111,13 +110,13 @@ public class TimestampPropertyMojo extends AbstractDefinePropertyMojo { * * @since 3.2.0 */ - @Parameter(readonly = true, defaultValue = "${session}") - private MavenSession mavenSession; + @Inject + private Session session; /** * {@inheritDoc} */ - public void execute() throws MojoExecutionException, MojoFailureException { + public void execute() throws MojoException, MojoException { Locale locale; if (this.locale != null) { String[] bits = this.locale.split("[,_]"); @@ -128,7 +127,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { } else if (bits.length == 3) { locale = new Locale(bits[0].trim(), bits[1].trim(), bits[2].trim()); } else { - throw new MojoExecutionException("expecting language,country,variant but got more than three parts"); + throw new MojoException("expecting language,country,variant but got more than three parts"); } } else { locale = Locale.getDefault(); @@ -143,7 +142,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { try { format = new SimpleDateFormat(pattern, locale); } catch (IllegalArgumentException e) { - throw new MojoExecutionException(e.getMessage(), e); + throw new MojoException(e.getMessage(), e); } } @@ -182,7 +181,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { format.setTimeZone(timeZone); if ("build".equals(timeSource)) { - defineProperty(name, format.format(mavenSession.getStartTime())); + defineProperty(name, format.format(new Date(session.getStartTime().toEpochMilli()))); } else { defineProperty(name, format.format(calendar.getTime())); } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/UpToDatePropertiesMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/UpToDatePropertiesMojo.java index cafd4be5..44ef29e0 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/UpToDatePropertiesMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/UpToDatePropertiesMojo.java @@ -26,11 +26,9 @@ import java.util.List; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.api.plugin.MojoException; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; /** * Sets multiple properties according to whether multiple sets of source and target resources are respectively up to @@ -39,7 +37,7 @@ * @author Adrian Price demonfiddler@virginmedia.com * @since 1.12 */ -@Mojo(name = "uptodate-properties", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true) +@Mojo(name = "uptodate-properties", defaultPhase = "validate") public class UpToDatePropertiesMojo extends AbstractUpToDatePropertyMojo { /** * List of UpToDatePropertySettings to apply. @@ -55,7 +53,7 @@ public class UpToDatePropertiesMojo extends AbstractUpToDatePropertyMojo { /** {@inheritDoc} */ @Override - public void execute() throws MojoExecutionException, MojoFailureException { + public void execute() throws MojoException, MojoException { if (skip) { getLog().info("uptodate-properties is skipped."); return; diff --git a/src/main/java/org/codehaus/mojo/buildhelper/UpToDatePropertyMojo.java b/src/main/java/org/codehaus/mojo/buildhelper/UpToDatePropertyMojo.java index 5d855975..db098a8c 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/UpToDatePropertyMojo.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/UpToDatePropertyMojo.java @@ -24,11 +24,9 @@ * SOFTWARE. */ -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.api.plugin.MojoException; +import org.apache.maven.api.plugin.annotations.Mojo; +import org.apache.maven.api.plugin.annotations.Parameter; import org.apache.maven.shared.model.fileset.FileSet; /** @@ -37,7 +35,7 @@ * @author Adrian Price demonfiddler@virginmedia.com * @since 1.12 */ -@Mojo(name = "uptodate-property", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true) +@Mojo(name = "uptodate-property", defaultPhase = "validate") public class UpToDatePropertyMojo extends AbstractUpToDatePropertyMojo { /** * The name of the property to set. @@ -71,7 +69,7 @@ public class UpToDatePropertyMojo extends AbstractUpToDatePropertyMojo { /** {@inheritDoc} */ @Override - public void execute() throws MojoExecutionException, MojoFailureException { + public void execute() throws MojoException, MojoException { if (skip) { getLog().info("uptodate-property is skipped."); return; diff --git a/src/main/java/org/codehaus/mojo/buildhelper/UpToDatePropertySetting.java b/src/main/java/org/codehaus/mojo/buildhelper/UpToDatePropertySetting.java index f1e23e0a..b10b775a 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/UpToDatePropertySetting.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/UpToDatePropertySetting.java @@ -24,7 +24,7 @@ * SOFTWARE. */ -import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.api.plugin.annotations.Parameter; import org.apache.maven.shared.model.fileset.FileSet; import org.codehaus.plexus.util.StringUtils; @@ -93,7 +93,7 @@ public void setElse(String elseValue) { this.elseValue = elseValue; } - void validate() { + public void validate() { if (StringUtils.isBlank(name)) { throw new IllegalArgumentException("name required"); } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/Artifact.java b/src/main/java/org/codehaus/mojo/buildhelper/utils/Artifact.java similarity index 89% rename from src/main/java/org/codehaus/mojo/buildhelper/Artifact.java rename to src/main/java/org/codehaus/mojo/buildhelper/utils/Artifact.java index 45c179ef..82d99c21 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/Artifact.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/utils/Artifact.java @@ -1,4 +1,4 @@ -package org.codehaus.mojo.buildhelper; +package org.codehaus.mojo.buildhelper.utils; /* * The MIT License @@ -24,7 +24,7 @@ * SOFTWARE. */ -import java.io.File; +import java.nio.file.Path; /** *

@@ -34,7 +34,7 @@ * @author dtran */ public class Artifact { - private File file; + private Path path; private String type = "jar"; @@ -47,8 +47,8 @@ public class Artifact { * * @param localFile a {@link java.io.File} object. */ - public void setFile(File localFile) { - this.file = localFile; + public void setPath(Path localFile) { + this.path = localFile; } /** @@ -56,10 +56,10 @@ public void setFile(File localFile) { * Getter for the field file. *

* - * @return a {@link java.io.File} object. + * @return a {@link java.nio.file.Path} object. */ - public File getFile() { - return this.file; + public Path getPath() { + return this.path; } /** diff --git a/src/main/java/org/codehaus/mojo/buildhelper/versioning/VersionInformation.java b/src/main/java/org/codehaus/mojo/buildhelper/utils/ParsedVersion.java similarity index 81% rename from src/main/java/org/codehaus/mojo/buildhelper/versioning/VersionInformation.java rename to src/main/java/org/codehaus/mojo/buildhelper/utils/ParsedVersion.java index a0e3dc9d..6bd772ac 100644 --- a/src/main/java/org/codehaus/mojo/buildhelper/versioning/VersionInformation.java +++ b/src/main/java/org/codehaus/mojo/buildhelper/utils/ParsedVersion.java @@ -1,4 +1,4 @@ -package org.codehaus.mojo.buildhelper.versioning; +package org.codehaus.mojo.buildhelper.utils; /* * The MIT License @@ -33,7 +33,7 @@ * @author Karl Heinz Marbaise khmarbaise@apache.org * */ -public class VersionInformation { +public class ParsedVersion { private static final String MAJOR_MINOR_PATCH_PATTERN = "^((\\d+)(\\.(\\d+)(\\.(\\d+))?)?)"; private static final Pattern MAJOR_MINOR_PATCH = Pattern.compile(MAJOR_MINOR_PATCH_PATTERN); @@ -42,6 +42,8 @@ public class VersionInformation { private static final Pattern BUILD_NUMBER = Pattern.compile("(((\\-)(\\d+)(.*))?)|(\\.(.*))|(\\-(.*))|(.*)$"); + private String version; + private int major; private int minor; @@ -100,7 +102,8 @@ private void parseMajorMinorPatchVersion(String version) { } } - public VersionInformation(String version) { + public ParsedVersion(String version) { + this.version = version; Matcher matcherDigits = DIGITS.matcher(version); if (matcherDigits.matches()) { parseMajorMinorPatchVersion(matcherDigits.group(1)); @@ -149,4 +152,31 @@ public String getQualifier() { public void setQualifier(String qualifier) { this.qualifier = qualifier; } + + public String getAsOSGiVersion() { + StringBuilder osgiVersion = new StringBuilder(); + osgiVersion.append(this.getMajor()); + osgiVersion.append("." + this.getMinor()); + osgiVersion.append("." + this.getPatch()); + + if (this.getQualifier() != null || this.getBuildNumber() != 0) { + osgiVersion.append("."); + + if (this.getBuildNumber() != 0) { + osgiVersion.append(this.getBuildNumber()); + } + if (this.getQualifier() != null) { + // Do not allow having "." in it cause it's not allowed in OSGi. + String qualifier = this.getQualifier().replaceAll("\\.", "_"); + osgiVersion.append(qualifier); + } + } + + return osgiVersion.toString(); + } + + @Override + public String toString() { + return version; + } } diff --git a/src/main/java/org/codehaus/mojo/buildhelper/utils/Resource.java b/src/main/java/org/codehaus/mojo/buildhelper/utils/Resource.java new file mode 100644 index 00000000..8f70ab17 --- /dev/null +++ b/src/main/java/org/codehaus/mojo/buildhelper/utils/Resource.java @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.codehaus.mojo.buildhelper.utils; + +import java.util.ArrayList; +import java.util.List; + +/** + * @since 4.0 + */ +public class Resource { + + List includes; + List excludes; + String directory; + String targetPath; + boolean filtering; + String mergeId; + + public Resource() {} + + public List getIncludes() { + return includes; + } + + public void setIncludes(List includes) { + this.includes = includes; + } + + public List getExcludes() { + return excludes; + } + + public void setExcludes(List excludes) { + this.excludes = excludes; + } + + public String getDirectory() { + return directory; + } + + public void setDirectory(String directory) { + this.directory = directory; + } + + public String getTargetPath() { + return targetPath; + } + + public void setTargetPath(String targetPath) { + this.targetPath = targetPath; + } + + public boolean isFiltering() { + return filtering; + } + + public void setFiltering(boolean filtering) { + this.filtering = filtering; + } + + public String getMergeId() { + return mergeId; + } + + public void setMergeId(String mergeId) { + this.mergeId = mergeId; + } + + public void addInclude(String include) { + if (includes == null) { + includes = new ArrayList<>(); + } + includes.add(include); + } + + public void addExclude(String exclude) { + if (excludes == null) { + excludes = new ArrayList<>(); + } + excludes.add(exclude); + } +} diff --git a/src/main/java/org/codehaus/mojo/buildhelper/versioning/DefaultVersioning.java b/src/main/java/org/codehaus/mojo/buildhelper/versioning/DefaultVersioning.java deleted file mode 100644 index d0f953ca..00000000 --- a/src/main/java/org/codehaus/mojo/buildhelper/versioning/DefaultVersioning.java +++ /dev/null @@ -1,68 +0,0 @@ -package org.codehaus.mojo.buildhelper.versioning; - -/** - * @author Karl Heinz Marbaise khmarbaise@apache.org - */ -public class DefaultVersioning implements Versioning { - - private VersionInformation vi; - - private String version; - - public DefaultVersioning(String version) { - this.version = version; - this.vi = new VersionInformation(version); - } - - public String getVersion() { - return this.version; - } - - @Override - public int getMajor() { - return this.vi.getMajor(); - } - - @Override - public int getMinor() { - return this.vi.getMinor(); - } - - @Override - public int getPatch() { - return this.vi.getPatch(); - } - - @Override - public String getAsOSGiVersion() { - StringBuffer osgiVersion = new StringBuffer(); - osgiVersion.append(this.getMajor()); - osgiVersion.append("." + this.getMinor()); - osgiVersion.append("." + this.getPatch()); - - if (this.getQualifier() != null || this.getBuildNumber() != 0) { - osgiVersion.append("."); - - if (this.getBuildNumber() != 0) { - osgiVersion.append(this.getBuildNumber()); - } - if (this.getQualifier() != null) { - // Do not allow having "." in it cause it's not allowed in OSGi. - String qualifier = this.getQualifier().replaceAll("\\.", "_"); - osgiVersion.append(qualifier); - } - } - - return osgiVersion.toString(); - } - - @Override - public long getBuildNumber() { - return this.vi.getBuildNumber(); - } - - @Override - public String getQualifier() { - return this.vi.getQualifier(); - } -} diff --git a/src/main/java/org/codehaus/mojo/buildhelper/versioning/Versioning.java b/src/main/java/org/codehaus/mojo/buildhelper/versioning/Versioning.java deleted file mode 100644 index 056e038d..00000000 --- a/src/main/java/org/codehaus/mojo/buildhelper/versioning/Versioning.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.codehaus.mojo.buildhelper.versioning; - -public interface Versioning { - - int getMajor(); - - int getMinor(); - - int getPatch(); - - String getAsOSGiVersion(); - - long getBuildNumber(); - - String getQualifier(); -} diff --git a/src/test/java/org/codehaus/mojo/buildhelper/ParseVersionTest.java b/src/test/java/org/codehaus/mojo/buildhelper/ParseVersionTest.java index 3cf57ff9..89c267fe 100644 --- a/src/test/java/org/codehaus/mojo/buildhelper/ParseVersionTest.java +++ b/src/test/java/org/codehaus/mojo/buildhelper/ParseVersionTest.java @@ -24,8 +24,10 @@ * SOFTWARE. */ +import java.lang.reflect.Proxy; import java.util.Properties; +import org.apache.maven.api.plugin.Log; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -38,6 +40,13 @@ static class TestParseVersionMojo extends ParseVersionMojo { TestParseVersionMojo(Properties properties) { this.properties = properties; + this.log = (Log) + Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {Log.class}, (o, m, params) -> { + if (params.length == 1 && params[0] instanceof CharSequence) { + System.out.println("[" + m.getName() + "] " + params[0]); + } + return null; + }); } @Override diff --git a/src/test/java/org/codehaus/mojo/buildhelper/versioning/VersionInformationTest.java b/src/test/java/org/codehaus/mojo/buildhelper/versioning/ParsedVersionTest.java similarity index 93% rename from src/test/java/org/codehaus/mojo/buildhelper/versioning/VersionInformationTest.java rename to src/test/java/org/codehaus/mojo/buildhelper/versioning/ParsedVersionTest.java index fa04a48f..285f017b 100644 --- a/src/test/java/org/codehaus/mojo/buildhelper/versioning/VersionInformationTest.java +++ b/src/test/java/org/codehaus/mojo/buildhelper/versioning/ParsedVersionTest.java @@ -19,6 +19,7 @@ * under the License. */ +import org.codehaus.mojo.buildhelper.utils.ParsedVersion; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; @@ -29,7 +30,7 @@ /** * @author Karl Heinz Marbaise khmarbaise@apache.org */ -class VersionInformationTest { +class ParsedVersionTest { // @formatter:off private static Object[][] createVersions() { return new Object[][] { @@ -72,7 +73,7 @@ private static Object[][] createVersions() { @ParameterizedTest @MethodSource("createVersions") void checkVersions(String version, int major, int minor, int patch, long buildNumber, String qualifier) { - VersionInformation vi = new VersionInformation(version); + ParsedVersion vi = new ParsedVersion(version); assertEquals(vi.getMajor(), major); assertEquals(vi.getMinor(), minor); assertEquals(vi.getPatch(), patch); @@ -82,6 +83,6 @@ void checkVersions(String version, int major, int minor, int patch, long buildNu @Test void shouldFaileWithNumberFormatException() { - assertThrows(NumberFormatException.class, () -> new VersionInformation("999999999999.12345678.12.beta_5")); + assertThrows(NumberFormatException.class, () -> new ParsedVersion("999999999999.12345678.12.beta_5")); } }