Skip to content

Commit

Permalink
Add configuration to be able to use the plugin without scm info in pom
Browse files Browse the repository at this point in the history
Signed-off-by: Olivier Lamy <olamy@apache.org>
  • Loading branch information
olamy committed Sep 2, 2024
1 parent e8bee16 commit ac1b7ce
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/main/java/org/codehaus/mojo/build/AbstractScmMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
42 changes: 42 additions & 0 deletions src/test/projects/create-metadata-it-no-scm/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.codehaus.mojo.it</groupId>
<artifactId>build-metadata-it</artifactId>
<version>1.0-SNAPSHOT</version>

<build>

<defaultGoal>package</defaultGoal>

<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>${it-plugin.version}</version>
<executions>
<execution>
<id>useLastCommittedRevision</id>
<goals>
<goal>create-metadata</goal>
</goals>
<configuration>
<ignoreEmptyScmUrl>true</ignoreEmptyScmUrl>
<revisionOnScmFailure>UNKNOWN</revisionOnScmFailure>
<!-- see outputDirectory + outputName for the default outputFile -->
<outputFiles>
<outputFile>${project.build.directory}/file1.properties</outputFile>
<outputFile>${project.build.directory}/xxx/file1.properties</outputFile>
</outputFiles>
<addOutputDirectoryToResources>true</addOutputDirectoryToResources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

</build>

</project>

0 comments on commit ac1b7ce

Please sign in to comment.