From 4d1fc2538526848cd78f4d8275a99e87fe8a1406 Mon Sep 17 00:00:00 2001 From: Sebastian Westemeyer Date: Tue, 14 Dec 2021 21:35:21 +0100 Subject: [PATCH] Added url and description to generated artifacts. --- README.md | 6 +++--- pom.xml | 2 +- .../maven/versions/GenerateServiceMojo.java | 11 +++++++++-- .../plugins/maven/versions/service-template.txt | 2 +- .../maven/versions/GenerateServiceMojoTest.java | 16 ++++++++++------ .../de/westemeyer/plugins/maven/versions/pom.xml | 1 + 6 files changed, 25 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 37eebbb..0087567 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ It is used in combination with the [artifact-version-service](https://github.com de.westemeyer artifact-version-maven-plugin - 1.0.2 + 1.1.0 @@ -47,7 +47,7 @@ It is used in combination with the [artifact-version-service](https://github.com de.westemeyer artifact-version-service - 1.0.2 + 1.1.0 ``` @@ -59,7 +59,7 @@ It is also possible to configure the generator to use target directories and a m de.westemeyer artifact-version-maven-plugin - 1.0.2 + 1.1.0 diff --git a/pom.xml b/pom.xml index d0046c9..cf341d5 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ de.westemeyer artifact-version-maven-plugin - 1.0.2 + 1.1.0 Maven source code generator for artifact version services. The artifact-version-maven-plugin is used to automatically generate artifact version information to be collected by ArtifactVersionCollector somewhere in the classpath. diff --git a/src/main/java/de/westemeyer/plugins/maven/versions/GenerateServiceMojo.java b/src/main/java/de/westemeyer/plugins/maven/versions/GenerateServiceMojo.java index a12d619..283845b 100644 --- a/src/main/java/de/westemeyer/plugins/maven/versions/GenerateServiceMojo.java +++ b/src/main/java/de/westemeyer/plugins/maven/versions/GenerateServiceMojo.java @@ -200,8 +200,13 @@ void writeServiceClass() throws MojoFailureException { // iterate map of template values for replacement for (Map.Entry entry : getTemplateValues().entrySet()) { - // replace values in string - out = out.replace("${" + entry.getKey() + "}", entry.getValue()); + String value = entry.getValue(); + if (value == null) { + out = out.replace("\"${" + entry.getKey() + "}\"", "null"); + } else { + // replace values in string + out = out.replace("${" + entry.getKey() + "}", value); + } } // write resulting java source code to output file @@ -225,6 +230,8 @@ Map getTemplateValues() { valueMap.put("artifactId", project.getArtifactId()); valueMap.put("version", project.getVersion()); valueMap.put("name", project.getName()); + valueMap.put("url", project.getUrl()); + valueMap.put("description", project.getDescription()); valueMap.put("timestamp", "" + new Date().getTime()); return valueMap; } diff --git a/src/main/resources/de/westemeyer/plugins/maven/versions/service-template.txt b/src/main/resources/de/westemeyer/plugins/maven/versions/service-template.txt index 33952dd..b1bcdce 100644 --- a/src/main/resources/de/westemeyer/plugins/maven/versions/service-template.txt +++ b/src/main/resources/de/westemeyer/plugins/maven/versions/service-template.txt @@ -9,6 +9,6 @@ import de.westemeyer.version.service.ArtifactVersionService; public class ${serviceClass} implements ArtifactVersionService { @Override public Artifact getArtifact() { - return new Artifact("${groupId}", "${artifactId}", "${version}", ${timestamp}L, "${name}"); + return new Artifact("${groupId}", "${artifactId}", "${version}", ${timestamp}L, "${name}", "${description}", "${url}"); } } diff --git a/src/test/java/de/westemeyer/plugins/maven/versions/GenerateServiceMojoTest.java b/src/test/java/de/westemeyer/plugins/maven/versions/GenerateServiceMojoTest.java index 2e898c1..6d37f5c 100644 --- a/src/test/java/de/westemeyer/plugins/maven/versions/GenerateServiceMojoTest.java +++ b/src/test/java/de/westemeyer/plugins/maven/versions/GenerateServiceMojoTest.java @@ -1,6 +1,7 @@ package de.westemeyer.plugins.maven.versions; import org.apache.maven.model.Build; +import org.apache.maven.model.Model; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.testing.stubs.MavenProjectStub; import org.junit.jupiter.api.Assertions; @@ -99,7 +100,7 @@ void writeServiceClass() { mojo.setOutstreamBehaviour(OUTSTREAM_BEHAVIOUR.BYTE); mojo.setInstreamBehaviour(INSTREAM_BEHAVIOUR.BYTE); Assertions.assertDoesNotThrow(mojo::writeServiceClass); - Assertions.assertEquals("de.westemeyer:artifact-version-test:1.0.0-SNAPSHOT:My new maven project name:de.westemeyer.service.version:MyServiceClass", mojo.getOutputString().replace("\n", "")); + Assertions.assertEquals("\"de.westemeyer\":\"artifact-version-test\":\"1.0.0-SNAPSHOT\":\"My new maven project name\":\"de.westemeyer.service.version\":\"MyServiceClass\":\"https://www.myproject.com\":null", mojo.getOutputString().replace("\n", "")); } @Test @@ -146,10 +147,13 @@ private MockProject createMockProject() { build.setOutputDirectory("target/testdir"); project.setBuild(build); project.readModel(testPom); - project.setArtifactId(project.getModel().getArtifactId()); - project.setGroupId(project.getModel().getGroupId()); - project.setName(project.getModel().getName()); - project.setVersion((project.getModel().getVersion())); + Model model = project.getModel(); + project.setArtifactId(model.getArtifactId()); + project.setGroupId(model.getGroupId()); + project.setName(model.getName()); + project.setVersion(model.getVersion()); + project.setUrl(model.getUrl()); + project.setDescription(model.getDescription()); return project; } @@ -164,7 +168,7 @@ private static class MockGenerateServiceMojo extends GenerateServiceMojo { private OUTSTREAM_BEHAVIOUR outstreamBehaviour = OUTSTREAM_BEHAVIOUR.SUPER; private INSTREAM_BEHAVIOUR instreamBehaviour = INSTREAM_BEHAVIOUR.SUPER; private final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - private static final String TEMPLATE = "${groupId}:${artifactId}:${version}:${name}:${package}:${serviceClass}"; + private static final String TEMPLATE = "\"${groupId}\":\"${artifactId}\":\"${version}\":\"${name}\":\"${package}\":\"${serviceClass}\":\"${url}\":\"${description}\""; private final ByteArrayInputStream inputStream = new ByteArrayInputStream(TEMPLATE.getBytes(StandardCharsets.UTF_8)); @Override diff --git a/src/test/resources/de/westemeyer/plugins/maven/versions/pom.xml b/src/test/resources/de/westemeyer/plugins/maven/versions/pom.xml index ff6b38f..6bb9877 100644 --- a/src/test/resources/de/westemeyer/plugins/maven/versions/pom.xml +++ b/src/test/resources/de/westemeyer/plugins/maven/versions/pom.xml @@ -9,6 +9,7 @@ 1.0.0-SNAPSHOT My new maven project name + https://www.myproject.com 8