Skip to content

Commit

Permalink
Minor improvements regarding line feeds, file names and multi-module …
Browse files Browse the repository at this point in the history
…projects
  • Loading branch information
swesteme committed Mar 16, 2022
1 parent 4d1fc25 commit 1cdb051
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ It is used in combination with the [artifact-version-service](https://github.com
<plugin>
<groupId>de.westemeyer</groupId>
<artifactId>artifact-version-maven-plugin</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
<executions>
<execution>
<goals>
Expand Down Expand Up @@ -59,7 +59,7 @@ It is also possible to configure the generator to use target directories and a m
<plugin>
<groupId>de.westemeyer</groupId>
<artifactId>artifact-version-maven-plugin</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
<executions>
<execution>
<goals>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.westemeyer</groupId>
<artifactId>artifact-version-maven-plugin</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>

<name>Maven source code generator for artifact version services.</name>
<description>The artifact-version-maven-plugin is used to automatically generate artifact version information to be collected by ArtifactVersionCollector somewhere in the classpath.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,24 @@ public class GenerateServiceMojo extends AbstractMojo {

@Override
public void execute() throws MojoFailureException {
// optional package name parameter can be "guessed" from group ID
packageName = setUpParameterValue("Package name", packageName, () -> project.getGroupId() + ".versions");
String packaging = project.getPackaging();
// parent poms in multi-module projects do not need a service class
if (packaging == null || !packaging.equalsIgnoreCase("pom")) {
// optional package name parameter can be "guessed" from group ID
packageName = setUpParameterValue("Package name", packageName, () -> project.getGroupId() + ".versions");

// optional service class name parameter can be "guessed" from artifact ID
serviceClass = setUpParameterValue("Service class", serviceClass, this::determineServiceClassName);
// optional service class name parameter can be "guessed" from artifact ID
serviceClass = setUpParameterValue("Service class", serviceClass, this::determineServiceClassName);

// write the service class
writeServiceClass();
// write the service class
writeServiceClass();

// write the service manifest
writeServiceManifest();
// write the service manifest
writeServiceManifest();

// add source root for generated source file
project.addCompileSourceRoot(targetFolder.getPath());
// add source root for generated source file
project.addCompileSourceRoot(targetFolder.getPath());
}
}

/**
Expand Down Expand Up @@ -229,13 +233,23 @@ Map<String, String> getTemplateValues() {
valueMap.put("groupId", project.getGroupId());
valueMap.put("artifactId", project.getArtifactId());
valueMap.put("version", project.getVersion());
valueMap.put("name", project.getName());
valueMap.put("name", replaceLineFeeds(project.getName()));
valueMap.put("url", project.getUrl());
valueMap.put("description", project.getDescription());
valueMap.put("description", replaceLineFeeds(project.getDescription()));
valueMap.put("timestamp", "" + new Date().getTime());
return valueMap;
}

/**
* Strings in pom.xml files may contain line breaks. Replace these by backslash n to have valid String constants in
* generated service class files.
* @param input the input string from pom.xml
* @return correct string for service class
*/
static String replaceLineFeeds(String input) {
return input == null ? null : input.replaceAll("(\\t|\\r?\\n)+", "\\\\n");
}

/**
* Read service java class template file from resources.
*
Expand Down Expand Up @@ -294,7 +308,32 @@ protected OutputStream createServiceFileOutputStream(File serviceFile) throws Fi
* @throws FileNotFoundException in case the file can not be created
*/
protected OutputStream createServiceClassOutputStream(File packageDir) throws FileNotFoundException {
return new FileOutputStream(new File(packageDir, serviceClass + ".java"));
// concatenate service class file name
String fileName = serviceClass + ".java";

// create file object
File file = new File(packageDir, fileName);

// check, whether file exists under a name that is similar, but not equal to file name (case-insensitive)
if (file.exists() && !checkFileExistsCaseSensitive(file, fileName) && !file.delete()) {
getLog().warn("Unable to remove file with different name before generating new artifact version service file. Try cleaning project first.");
}
return new FileOutputStream(file);
}

/**
* Check if a file exists with another combination of uppercase and lowercase characters. May be important when
* generating a new service class on a Windows or macOS system.
* @param file the file to check
* @param fileName the expected file name to compare to
* @return whether a file with different spelling exists
*/
protected boolean checkFileExistsCaseSensitive(File file, String fileName) {
try {
return file.getCanonicalFile().getName().equals(fileName);
} catch (IOException exception) {
return false;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void getTemplateValues() {
Assertions.assertEquals("de.westemeyer.service.version", templateValues.get("package"));
Assertions.assertEquals("MyServiceClass", templateValues.get("serviceClass"));
Assertions.assertEquals("de.westemeyer", templateValues.get("groupId"));
Assertions.assertEquals("My new maven project name", templateValues.get("name"));
Assertions.assertEquals("My new maven\\nproject name", templateValues.get("name"));
Assertions.assertEquals("artifact-version-test", templateValues.get("artifactId"));
Assertions.assertEquals("1.0.0-SNAPSHOT", templateValues.get("version"));
Assertions.assertDoesNotThrow(() -> Long.valueOf(templateValues.get("timestamp")));
Expand Down Expand Up @@ -100,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\":\"https://www.myproject.com\":null", mojo.getOutputString().replace("\n", ""));
Assertions.assertEquals("\"de.westemeyer\":\"artifact-version-test\":\"1.0.0-SNAPSHOT\":\"My new maven\\nproject name\":\"de.westemeyer.service.version\":\"MyServiceClass\":\"https://www.myproject.com\":null", mojo.getOutputString().replace("\n", ""));
}

@Test
Expand All @@ -121,6 +121,22 @@ void execute() {
Assertions.assertTrue(mojo.project.getCompileSourceRoots().contains("target/testdir"));
}

@Test
void checkFileExistsCaseSensitive() {
File file = new File("target/testdir/de/westemeyer/service/version/MyServiceClass.java");
if (file.exists()) {
Assertions.assertTrue(file.delete());
}
MockGenerateServiceMojo mojo = createMojo();
Assertions.assertDoesNotThrow(mojo::writeServiceClass);
Assertions.assertTrue(mojo.checkFileExistsCaseSensitive(file, file.getName()));
Assertions.assertFalse(mojo.checkFileExistsCaseSensitive(file, "otherFileName.java"));
File file1 = new File(file.getParent(), "myServiceClass.java");
if (file1.exists()) {
Assertions.assertTrue(mojo.checkFileExistsCaseSensitive(file1, "MyServiceClass.java"));
}
}

private void assertServiceClassName(String artifactId, String expectedClassName) {
MockProject project = createMockProject();
project.setArtifactId(artifactId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
<artifactId>artifact-version-test</artifactId>
<version>1.0.0-SNAPSHOT</version>

<name>My new maven project name</name>
<name>My new maven
project name</name>
<url>https://www.myproject.com</url>

<properties>
Expand Down

0 comments on commit 1cdb051

Please sign in to comment.