Derive package name from groupId and artifactId

This commit derives the package name of the application from the
artifact's `groupId` and `artifactId`. Previously sources were put in a
package that mirrors the groupId value.

This goes against the "unique package per application" policy that we
try to enforce. Even if the package name value can be customized
manually, deriving it automatically from the values provided in those
fields will help structure codebases.

With this change, the package name is derived like this:
* groupId `com.example`, artifactId `bookmarks` -> package
`com.example.bookmarks`
* groupId `com.example`, artifactId `user-management` -> package
`com.example.usermanagement`

This commit fixes the package name generation on the server, but also
in the web interfaces when the user updates the form fields.

Fixes gh-421
This commit is contained in:
Brian Clozel
2017-05-11 14:36:47 +02:00
parent 40ba2ea75c
commit bb578d6fdf
27 changed files with 72 additions and 45 deletions
@@ -65,7 +65,7 @@ public class InitializrMetadata {
private final TextCapability version = new TextCapability("version", "Version",
"project version");
private final TextCapability packageName = new PackageCapability(groupId);
private final TextCapability packageName = new PackageCapability(groupId, artifactId);
public InitializrMetadata() {
this(new InitializrConfiguration());
@@ -282,19 +282,29 @@ public class InitializrMetadata {
}
private static class PackageCapability extends TextCapability {
private final TextCapability nameCapability;
private final TextCapability groupId;
private final TextCapability artifactId;
PackageCapability(TextCapability nameCapability) {
PackageCapability(TextCapability groupId, TextCapability artifactId) {
super("packageName", "Package Name", "root package");
this.nameCapability = nameCapability;
this.groupId = groupId;
this.artifactId = artifactId;
}
@Override
public String getContent() {
String value = super.getContent();
return value != null ? value
: (nameCapability.getContent() != null
? nameCapability.getContent().replace("-", ".") : null);
if (value != null) {
return value;
}
else if(this.groupId.getContent() != null && this.artifactId.getContent() != null) {
return this.groupId.getContent()
.concat(".")
.concat(this.artifactId.getContent())
.replaceAll("-", "")
.replaceAll("\\.[0-9]+", ".");
}
return null;
}
}
@@ -76,7 +76,7 @@ public class ProjectGeneratorLanguageTests extends AbstractProjectGeneratorTests
ProjectAssert project = generateProject(request);
project.sourceCodeAssert(
"src/main/" + language + "/com/example/DemoApplication." + extension)
"src/main/" + language + "/com/example/demo/DemoApplication." + extension)
.equalsTo(new ClassPathResource("project/" + language
+ "/standard/DemoApplication." + expectedExtension));
}
@@ -88,7 +88,7 @@ public class ProjectGeneratorLanguageTests extends AbstractProjectGeneratorTests
ProjectAssert project = generateProject(request);
project.sourceCodeAssert(
"src/test/" + language + "/com/example/DemoApplicationTests." + extension)
"src/test/" + language + "/com/example/demo/DemoApplicationTests." + extension)
.equalsTo(new ClassPathResource("project/" + language
+ "/standard/DemoApplicationTests." + expectedExtension));
}
@@ -100,7 +100,7 @@ public class ProjectGeneratorLanguageTests extends AbstractProjectGeneratorTests
ProjectAssert project = generateProject(request);
project.sourceCodeAssert(
"src/test/" + language + "/com/example/DemoApplicationTests." + extension)
"src/test/" + language + "/com/example/demo/DemoApplicationTests." + extension)
.equalsTo(new ClassPathResource("project/" + language
+ "/standard/DemoApplicationTestsWeb." + expectedExtension));
}
@@ -129,7 +129,7 @@ public class ProjectGeneratorLanguageTests extends AbstractProjectGeneratorTests
}
ProjectAssert project = generateProject(request);
project.sourceCodeAssert(
"src/main/" + language + "/com/example/ServletInitializer." + extension)
"src/main/" + language + "/com/example/demo/ServletInitializer." + extension)
.equalsTo(new ClassPathResource("project/" + language
+ "/" + expectedOutput + "/ServletInitializer." + expectedExtension));
}
@@ -142,7 +142,7 @@ public class ProjectGeneratorLanguageTests extends AbstractProjectGeneratorTests
ProjectAssert project = generateProject(request);
project.sourceCodeAssert(
"src/test/" + language + "/com/example/DemoApplicationTests." + extension)
"src/test/" + language + "/com/example/demo/DemoApplicationTests." + extension)
.equalsTo(new ClassPathResource("project/" + language
+ "/standard/DemoApplicationTests." + expectedExtension));
}
@@ -155,7 +155,7 @@ public class ProjectGeneratorLanguageTests extends AbstractProjectGeneratorTests
ProjectAssert project = generateProject(request);
project.sourceCodeAssert(
"src/test/" + language + "/com/example/DemoApplicationTests." + extension)
"src/test/" + language + "/com/example/demo/DemoApplicationTests." + extension)
.equalsTo(new ClassPathResource("project/" + language
+ "/spring-boot-1.4/DemoApplicationTests." + expectedExtension));
}
@@ -168,7 +168,7 @@ public class ProjectGeneratorLanguageTests extends AbstractProjectGeneratorTests
ProjectAssert project = generateProject(request);
project.sourceCodeAssert(
"src/test/" + language + "/com/example/DemoApplicationTests." + extension)
"src/test/" + language + "/com/example/demo/DemoApplicationTests." + extension)
.equalsTo(new ClassPathResource("project/" + language
+ "/spring-boot-1.4/DemoApplicationTests." + expectedExtension));
}
@@ -199,4 +199,17 @@ public class InitializrMetadataTests {
builder.build();
}
@Test
public void stripInvalidCharsFromPackage() {
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults().build();
metadata.getGroupId().setContent("org.acme");
metadata.getArtifactId().setContent("2foo.bar");
assertThat(metadata.getPackageName().getContent()).isEqualTo("org.acme.foo.bar");
metadata = InitializrMetadataTestBuilder.withDefaults().build();
metadata.getGroupId().setContent("org.ac-me");
metadata.getArtifactId().setContent("foo-bar");
assertThat(metadata.getPackageName().getContent()).isEqualTo("org.acme.foobar");
}
}
@@ -37,7 +37,7 @@ import static org.junit.Assert.assertTrue;
*/
public class ProjectAssert {
public static final String DEFAULT_PACKAGE_NAME = "com.example";
public static final String DEFAULT_PACKAGE_NAME = "com.example.demo";
public static final String DEFAULT_APPLICATION_NAME = "DemoApplication";
@@ -1,4 +1,4 @@
package com.example
package com.example.demo
import org.junit.Test
import org.junit.runner.RunWith
@@ -1,4 +1,4 @@
package com.example
package com.example.demo
import org.springframework.boot.builder.SpringApplicationBuilder
import org.springframework.boot.web.support.SpringBootServletInitializer
@@ -1,4 +1,4 @@
package com.example
package com.example.demo
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
@@ -1,4 +1,4 @@
package com.example
package com.example.demo
import org.junit.Test
import org.junit.runner.RunWith
@@ -1,4 +1,4 @@
package com.example
package com.example.demo
import org.junit.Test
import org.junit.runner.RunWith
@@ -1,4 +1,4 @@
package com.example
package com.example.demo
import org.springframework.boot.builder.SpringApplicationBuilder
import org.springframework.boot.context.web.SpringBootServletInitializer
@@ -1,4 +1,4 @@
package com.example;
package com.example.demo;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -1,4 +1,4 @@
package com.example;
package com.example.demo;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@@ -1,4 +1,4 @@
package com.example;
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -1,4 +1,4 @@
package com.example;
package com.example.demo;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -1,4 +1,4 @@
package com.example;
package com.example.demo;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -1,4 +1,4 @@
package com.example;
package com.example.demo;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
@@ -1,4 +1,4 @@
package com.example
package com.example.demo
import org.junit.Test
import org.junit.runner.RunWith
@@ -1,4 +1,4 @@
package com.example
package com.example.demo
import org.springframework.boot.builder.SpringApplicationBuilder
import org.springframework.boot.web.support.SpringBootServletInitializer
@@ -1,4 +1,4 @@
package com.example
package com.example.demo
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
@@ -1,4 +1,4 @@
package com.example
package com.example.demo
import org.junit.Test
import org.junit.runner.RunWith
@@ -1,4 +1,4 @@
package com.example
package com.example.demo
import org.junit.Test
import org.junit.runner.RunWith
@@ -1,4 +1,4 @@
package com.example
package com.example.demo
import org.springframework.boot.builder.SpringApplicationBuilder
import org.springframework.boot.context.web.SpringBootServletInitializer