From ac1b7cebeb191d85e61512089190f618a99ba808 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Mon, 2 Sep 2024 21:18:01 +1000 Subject: [PATCH] Add configuration to be able to use the plugin without scm info in pom Signed-off-by: Olivier Lamy --- .../codehaus/mojo/build/AbstractScmMojo.java | 17 +++++++- .../mojo/build/it/CreateMetadataMojoTest.java | 15 +++++++ .../create-metadata-it-no-scm/pom.xml | 42 +++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/test/projects/create-metadata-it-no-scm/pom.xml diff --git a/src/main/java/org/codehaus/mojo/build/AbstractScmMojo.java b/src/main/java/org/codehaus/mojo/build/AbstractScmMojo.java index 68a30c2..da4c8c6 100644 --- a/src/main/java/org/codehaus/mojo/build/AbstractScmMojo.java +++ b/src/main/java/org/codehaus/mojo/build/AbstractScmMojo.java @@ -135,6 +135,20 @@ public abstract class AbstractScmMojo extends AbstractMojo { @Component protected ScmManager scmManager; + /** + * Ignore error when scm url from pom is empty and replace by scm:scmProvider {@link #scmProvider} + * @since 3.2.1 + */ + @Parameter(property = "maven.buildNumber.ignoreEmptyScmUrl", defaultValue = "false") + private boolean ignoreEmptyScmUrl; + + /** + * When scm url is empty, scm provider is needed {@link #ignoreEmptyScmUrl} + * @since 3.2.1 + */ + @Parameter(property = "maven.buildNumber.scmProvider", defaultValue = "git") + private String scmProvider; + /** * Maven Security Dispatcher * @@ -176,8 +190,9 @@ private String decrypt(String str, String server) { } protected ScmRepository getScmRepository() throws ScmException { + String repoUrl = !StringUtils.isBlank(this.scmConnectionUrl) ? scmConnectionUrl : scmDeveloperConnectionUrl; ScmRepository repository = scmManager.makeScmRepository( - !StringUtils.isBlank(this.scmConnectionUrl) ? scmConnectionUrl : scmDeveloperConnectionUrl); + StringUtils.isBlank(repoUrl) ? (ignoreEmptyScmUrl ? "scm:" + scmProvider + ":" : "") : repoUrl); ScmProviderRepository scmRepo = repository.getProviderRepository(); diff --git a/src/test/java/org/codehaus/mojo/build/it/CreateMetadataMojoTest.java b/src/test/java/org/codehaus/mojo/build/it/CreateMetadataMojoTest.java index 0764c8d..eea6ec0 100644 --- a/src/test/java/org/codehaus/mojo/build/it/CreateMetadataMojoTest.java +++ b/src/test/java/org/codehaus/mojo/build/it/CreateMetadataMojoTest.java @@ -55,4 +55,19 @@ public void testBasicJsonConfiguration() throws Exception { Assert.assertTrue(new File(testDir, "target/generated/build-metadata/build.properties").exists()); Assert.assertTrue(new File(testDir, "target/classes/build.properties").exists()); } + + @Test + public void testBasicConfigurationNoScm() throws Exception { + File projDir = resources.getBasedir("create-metadata-it-no-scm"); + + MavenExecution mavenExec = maven.forProject(projDir); + MavenExecutionResult result = mavenExec.withCliOption("-e").execute("clean", "test"); + result.assertErrorFreeLog(); + + File testDir = result.getBasedir(); + Assert.assertTrue(new File(testDir, "target/file1.properties").exists()); + Assert.assertTrue(new File(testDir, "target/xxx/file1.properties").exists()); + Assert.assertTrue(new File(testDir, "target/generated/build-metadata/build.properties").exists()); + Assert.assertTrue(new File(testDir, "target/classes/build.properties").exists()); + } } diff --git a/src/test/projects/create-metadata-it-no-scm/pom.xml b/src/test/projects/create-metadata-it-no-scm/pom.xml new file mode 100644 index 0000000..88d3e92 --- /dev/null +++ b/src/test/projects/create-metadata-it-no-scm/pom.xml @@ -0,0 +1,42 @@ + + + + 4.0.0 + + org.codehaus.mojo.it + build-metadata-it + 1.0-SNAPSHOT + + + + package + + + + org.codehaus.mojo + buildnumber-maven-plugin + ${it-plugin.version} + + + useLastCommittedRevision + + create-metadata + + + true + UNKNOWN + + + ${project.build.directory}/file1.properties + ${project.build.directory}/xxx/file1.properties + + true + + + + + + + + + \ No newline at end of file