Enable kotlin jpa plugin if necessary

This commit add the jpa kotlin plugin in the generated project when a
dependencies has the jpa facet.

See gh-728
This commit is contained in:
Rui Figueira
2018-09-04 23:13:00 +01:00
committed by Stephane Nicoll
parent 282b7a9916
commit 5f79f9a0fe
7 changed files with 119 additions and 0 deletions

View File

@@ -467,6 +467,11 @@ public class ProjectGenerator {
// Java versions // Java versions
model.put("java8OrLater", isJava8OrLater(request)); model.put("java8OrLater", isJava8OrLater(request));
// Has JPA facet
if (request.hasJpaFacet()) {
model.put("jpaFacet", true);
}
// Append the project request to the model // Append the project request to the model
BeanWrapperImpl bean = new BeanWrapperImpl(request); BeanWrapperImpl bean = new BeanWrapperImpl(request);
for (PropertyDescriptor descriptor : bean.getPropertyDescriptors()) { for (PropertyDescriptor descriptor : bean.getPropertyDescriptors()) {

View File

@@ -312,6 +312,14 @@ public class ProjectRequest extends BasicProjectRequest {
return hasFacet("web"); return hasFacet("web");
} }
/**
* Specify if this request has the jpa facet enabled.
* @return {@code true} if the project has the jpa facet
*/
public boolean hasJpaFacet() {
return hasFacet("jpa");
}
/** /**
* Specify if this request has the specified facet enabled. * Specify if this request has the specified facet enabled.
* @param facet the facet to check * @param facet the facet to check

View File

@@ -19,6 +19,9 @@ buildscript {
{{#kotlin}} {{#kotlin}}
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}") classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}") classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
{{#jpaFacet}}
classpath("org.jetbrains.kotlin:kotlin-noarg:${kotlinVersion}")
{{/jpaFacet}}
{{/kotlin}} {{/kotlin}}
} }
} }
@@ -26,6 +29,9 @@ buildscript {
apply plugin: '{{language}}' apply plugin: '{{language}}'
{{#kotlin}} {{#kotlin}}
apply plugin: 'kotlin-spring' apply plugin: 'kotlin-spring'
{{#jpaFacet}}
apply plugin: 'kotlin-jpa'
{{/jpaFacet}}
{{/kotlin}} {{/kotlin}}
{{#war}} {{#war}}
apply plugin: 'eclipse-wtp' apply plugin: 'eclipse-wtp'

View File

@@ -195,6 +195,9 @@
</args> </args>
<compilerPlugins> <compilerPlugins>
<plugin>spring</plugin> <plugin>spring</plugin>
{{#jpaFacet}}
<plugin>jpa</plugin>
{{/jpaFacet}}
</compilerPlugins> </compilerPlugins>
{{^kotlinSupport}} {{^kotlinSupport}}
{{#java8OrLater}} {{#java8OrLater}}

View File

@@ -960,4 +960,92 @@ public class ProjectGeneratorTests extends AbstractProjectGeneratorTests {
} }
} }
@Test
public void kotlinWithMavenUseJpaFacetHasJpaKotlinPlugin() {
Dependency jpa = Dependency.withId("data-jpa");
jpa.setFacets(Collections.singletonList("jpa"));
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
.addDependencyGroup("data-jpa", jpa).build();
applyMetadata(metadata);
ProjectRequest request = createProjectRequest("data-jpa");
request.setType("maven-project");
request.setLanguage("kotlin");
assertThat(generateMavenPom(request).getMavenPom())
.contains("<plugin>jpa</plugin>");
}
@Test
public void kotlinWithMavenWithoutJpaFacetDoesNotHaveJpaKotlinPlugin() {
Dependency jpa = Dependency.withId("data-jpa");
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
.addDependencyGroup("data-jpa", jpa).build();
applyMetadata(metadata);
ProjectRequest request = createProjectRequest("data-jpa");
request.setType("maven-project");
request.setLanguage("kotlin");
assertThat(generateMavenPom(request).getMavenPom())
.doesNotContain("<plugin>jpa</plugin>");
}
@Test
public void javaWithMavenUseJpaFacetDoesNotHaveJpaKotlinPlugin() {
Dependency jpa = Dependency.withId("data-jpa");
jpa.setFacets(Collections.singletonList("jpa"));
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
.addDependencyGroup("data-jpa", jpa).build();
applyMetadata(metadata);
ProjectRequest request = createProjectRequest("data-jpa");
request.setType("maven-project");
request.setLanguage("java");
assertThat(generateMavenPom(request).getMavenPom())
.doesNotContain("<plugin>jpa</plugin>");
}
@Test
public void kotlinWithGradleUseJpaFacetHasJpaKotlinPlugin() {
Dependency jpa = Dependency.withId("data-jpa");
jpa.setFacets(Collections.singletonList("jpa"));
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
.addDependencyGroup("data-jpa", jpa).build();
applyMetadata(metadata);
ProjectRequest request = createProjectRequest("data-jpa");
request.setType("gradle-project");
request.setLanguage("kotlin");
assertThat(generateGradleBuild(request).getGradleBuild())
.contains("apply plugin: 'kotlin-jpa'");
}
@Test
public void kotlinWithGradleWithoutJpaFacetDoesNotHaveJpaKotlinPlugin() {
Dependency jpa = Dependency.withId("data-jpa");
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
.addDependencyGroup("data-jpa", jpa).build();
applyMetadata(metadata);
ProjectRequest request = createProjectRequest("data-jpa");
request.setType("gradle-project");
request.setLanguage("kotlin");
assertThat(generateGradleBuild(request).getGradleBuild())
.doesNotContain("apply plugin: 'kotlin-jpa'");
}
@Test
public void javaWithGradleUseJpaFacetDoesNotHaveJpaKotlinPlugin() {
Dependency jpa = Dependency.withId("data-jpa");
jpa.setFacets(Collections.singletonList("jpa"));
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
.addDependencyGroup("data-jpa", jpa).build();
applyMetadata(metadata);
ProjectRequest request = createProjectRequest("data-jpa");
request.setType("gradle-project");
request.setLanguage("java");
assertThat(generateGradleBuild(request).getGradleBuild())
.doesNotContain("apply plugin: 'kotlin-jpa'");
}
} }

View File

@@ -46,6 +46,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class PomAssert { public class PomAssert {
private final String content;
private final XpathEngine eng; private final XpathEngine eng;
private final Document doc; private final Document doc;
@@ -61,6 +63,7 @@ public class PomAssert {
private final Map<String, Repository> repositories = new LinkedHashMap<>(); private final Map<String, Repository> repositories = new LinkedHashMap<>();
public PomAssert(String content) { public PomAssert(String content) {
this.content = content;
this.eng = XMLUnit.newXpathEngine(); this.eng = XMLUnit.newXpathEngine();
Map<String, String> context = new LinkedHashMap<>(); Map<String, String> context = new LinkedHashMap<>();
context.put("pom", "http://maven.apache.org/POM/4.0.0"); context.put("pom", "http://maven.apache.org/POM/4.0.0");
@@ -298,6 +301,10 @@ public class PomAssert {
return this; return this;
} }
public String getMavenPom() {
return this.content;
}
private PomAssert hasPluginRepository(String name) { private PomAssert hasPluginRepository(String name) {
NodeList nodes; NodeList nodes;
try { try {

View File

@@ -481,6 +481,8 @@ initializr:
id: data-jpa id: data-jpa
description: Java Persistence API including spring-data-jpa, spring-orm and Hibernate description: Java Persistence API including spring-data-jpa, spring-orm and Hibernate
weight: 100 weight: 100
facets:
- jpa
aliases: aliases:
- jpa - jpa
links: links: