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
#228)

* Add configuration to be able to use the plugin without scm info in pom

---------

Signed-off-by: Olivier Lamy <[email protected]>
  • Loading branch information
olamy authored Sep 3, 2024
1 parent e8bee16 commit eb58403
Show file tree
Hide file tree
Showing 4 changed files with 75 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
2 changes: 2 additions & 0 deletions src/site/fml/faq.fml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ under the License.
<answer>
<p>Yes, set the plugin parameter </p> <source> &lt;revisionOnScmFailure&gt;</source> <p>to prevent
the plugin from throwing an exception if SCM information is not found.</p>
<p>The other option is to use <source>-Dmaven.buildNumber.ignoreEmptyScmUrl=true -Dmaven.buildNumber.scmProvider=git/svn/</source>
Default is git</p>
</answer>
</faq>

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 eb58403

Please sign in to comment.