Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data data-tx if hibernate-jpa #2173

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,17 @@ public boolean hasDependencyInScope(@NonNull String groupId,
dependency.getArtifactId().equals(artifactId)
&& dependency.getScope() == scope);
}

public boolean hasDependency(@NonNull String groupId,
@NonNull String artifactId) {
return getDependencies().stream()
.anyMatch(dependency -> dependency.getGroupId().equals(groupId) &&
dependency.getArtifactId().equals(artifactId));
}

public long countDependencies(@NonNull String groupId) {
return getDependencies().stream()
.filter(dependency -> dependency.getGroupId().equals(groupId))
.count();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import io.micronaut.starter.options.BuildTool;

public final class MicronautDependencyUtils {
public static final String ARTIFACT_ID_MICRONAUT_DATA_TX_HIBERNATE = "micronaut-data-tx-hibernate";
public static final String ARTIFACT_ID_MICRONAUT_DATA_PROCESSOR_ARTIFACT = "micronaut-data-processor";
public static final String ARTIFACT_ID_MICRONAUT_INJECT = "micronaut-inject";
public static final Dependency MICRONAUT_INJECT = coreDependency()
.artifactId(ARTIFACT_ID_MICRONAUT_INJECT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
package io.micronaut.starter.feature.build;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.version.SemanticVersion;
import io.micronaut.starter.application.ApplicationType;
import io.micronaut.starter.application.generator.GeneratorContext;
import io.micronaut.starter.build.Property;
import io.micronaut.starter.build.S01SonatypeSnapshots;
import io.micronaut.starter.build.dependencies.Coordinate;
import io.micronaut.starter.build.dependencies.CoordinateResolver;
import io.micronaut.starter.build.dependencies.MicronautDependencyUtils;
import io.micronaut.starter.build.gradle.GradleDsl;
import io.micronaut.starter.build.gradle.GradlePlugin;
import io.micronaut.starter.build.gradle.GradlePluginPortal;
Expand All @@ -46,11 +48,11 @@
import io.micronaut.starter.feature.testresources.TestResources;
import io.micronaut.starter.options.Options;
import jakarta.inject.Singleton;

import java.util.Optional;
import java.util.Set;

import static io.micronaut.starter.feature.graalvm.GraalVM.FEATURE_NAME_GRAALVM;
import static io.micronaut.starter.build.dependencies.MicronautDependencyUtils.ARTIFACT_ID_MICRONAUT_DATA_PROCESSOR_ARTIFACT;

@Singleton
public class MicronautBuildPlugin implements BuildPluginFeature, DefaultFeature {
Expand Down Expand Up @@ -129,11 +131,21 @@ Optional<String> resolveRuntime(GeneratorContext generatorContext) {
.findFirst();
}

@Nullable
private Set<String> ignoredAutomaticDependencies(GeneratorContext generatorContext) {
if (generatorContext.hasDependency(MicronautDependencyUtils.GROUP_ID_MICRONAUT_DATA, MicronautDependencyUtils.ARTIFACT_ID_MICRONAUT_DATA_TX_HIBERNATE)
&& generatorContext.countDependencies(MicronautDependencyUtils.GROUP_ID_MICRONAUT_DATA) == 1) {
return Set.of(MicronautDependencyUtils.GROUP_ID_MICRONAUT_DATA + ":" + ARTIFACT_ID_MICRONAUT_DATA_PROCESSOR_ARTIFACT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is breaking the hibernate-jpa guide for gradle micronaut-projects/micronaut-guides#1369

}
return null;
}

protected MicronautApplicationGradlePlugin.Builder micronautGradleApplicationPluginBuilder(GeneratorContext generatorContext, String id) {
MicronautApplicationGradlePlugin.Builder builder = MicronautApplicationGradlePlugin.builder()
.buildTool(generatorContext.getBuildTool())
.incremental(true)
.packageName(generatorContext.getProject().getPackageName());
.packageName(generatorContext.getProject().getPackageName())
.ignoredAutomaticDependencies(ignoredAutomaticDependencies(generatorContext));
generatorContext.getFeatures()
.getFeatures()
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MicronautApplicationGradlePlugin {

Expand Down Expand Up @@ -58,11 +59,18 @@ public static final class Builder {
private String packageName;
private boolean sharedTestResources;

private Set<String> ignoredAutomaticDependencies;

public Builder buildTool(BuildTool buildTool) {
this.buildTool = buildTool;
return this;
}

public Builder ignoredAutomaticDependencies(Set<String> ignoredAutomaticDependencies) {
this.ignoredAutomaticDependencies = ignoredAutomaticDependencies;
return this;
}

public Builder incremental(boolean incremental) {
this.incremental = incremental;
return this;
Expand Down Expand Up @@ -144,7 +152,7 @@ public GradlePlugin.Builder builder() {
return GradlePlugin.builder()
.id(id)
.lookupArtifactId(ARTIFACT_ID)
.extension(new RockerWritable(micronautGradle.template(dsl, buildTool, dockerfile, dockerfileNative, dockerBuildImages, dockerBuildNativeImages, runtime, testRuntime, aotVersion, incremental, packageName, additionalTestResourceModules, sharedTestResources, aotKeys, lambdaRuntimeMainClass)));
.extension(new RockerWritable(micronautGradle.template(dsl, buildTool, dockerfile, dockerfileNative, dockerBuildImages, dockerBuildNativeImages, runtime, testRuntime, aotVersion, incremental, packageName, additionalTestResourceModules, sharedTestResources, aotKeys, lambdaRuntimeMainClass, ignoredAutomaticDependencies)));
}

public Builder dsl(GradleDsl gradleDsl) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import java.util.List
@import java.util.Set
@import java.util.Map
@import io.micronaut.starter.feature.build.gradle.Dockerfile
@import io.micronaut.starter.options.BuildTool
Expand All @@ -18,9 +19,15 @@
List<String> additionalTestResourceModules,
boolean sharedTestResources,
Map<String, String> aotKeys,
String lambdaRuntimeMainClass
String lambdaRuntimeMainClass,
Set<String> ignoredAutomaticDependencies
)
micronaut {
@if(ignoredAutomaticDependencies != null) {
@for (String ignoredAutomaticDependency : ignoredAutomaticDependencies) {
ignoredAutomaticDependencies.add("@(ignoredAutomaticDependency)")
}
}
@if(runtime != null) {
runtime("@(runtime)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
import java.util.LinkedHashMap;
import java.util.Map;

import static io.micronaut.starter.build.dependencies.MicronautDependencyUtils.ARTIFACT_ID_MICRONAUT_DATA_PROCESSOR_ARTIFACT;
import static io.micronaut.starter.build.dependencies.MicronautDependencyUtils.GROUP_ID_MICRONAUT_DATA;

public interface DataFeature extends OneOfFeature {

String SCHEMA_GENERATE_KEY = "datasources.default.schema-generate";
String MICRONAUT_DATA_VERSION = "micronaut.data.version";
String MICRONAUT_DATA_PROCESSOR_ARTIFACT = "micronaut-data-processor";

@Override
default Class<?> getFeatureClass() {
Expand All @@ -51,7 +51,7 @@ default Map<String, Object> getDatasourceConfig(GeneratorContext generatorContex
}

static Dependency dataProcessorDependency(BuildTool buildTool) {
return dataProcessorDependency(buildTool, MICRONAUT_DATA_PROCESSOR_ARTIFACT, Priority.MICRONAUT_DATA_PROCESSOR.getOrder());
return dataProcessorDependency(buildTool, ARTIFACT_ID_MICRONAUT_DATA_PROCESSOR_ARTIFACT, Priority.MICRONAUT_DATA_PROCESSOR.getOrder());
}

static Dependency dataProcessorDependency(BuildTool buildTool, String artifactId, int order) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import io.micronaut.starter.application.ApplicationType;
import io.micronaut.starter.application.generator.GeneratorContext;
import io.micronaut.starter.build.dependencies.Dependency;
import io.micronaut.starter.build.dependencies.MicronautDependencyUtils;
import io.micronaut.starter.feature.Category;
import io.micronaut.starter.feature.FeatureContext;
import io.micronaut.starter.feature.database.jdbc.JdbcFeature;
Expand All @@ -28,6 +28,7 @@
@Singleton
public class HibernateJpa implements JpaFeature {

private static final String ARTIFACT_ID_MICRONAUT_HIBERNATE_JPA = "micronaut-hibernate-jpa";
private final JdbcFeature jdbcFeature;

public HibernateJpa(JdbcFeature jdbcFeature) {
Expand Down Expand Up @@ -61,9 +62,15 @@ public void apply(GeneratorContext generatorContext) {
generatorContext.getConfiguration().put(JPA_HIBERNATE_PROPERTIES_HBM2DDL,
generatorContext.getFeatures().hasFeature(MigrationFeature.class) ? Hbm2ddlAuto.NONE.toString() :
Hbm2ddlAuto.UPDATE.toString());
generatorContext.addDependency(Dependency.builder()
.groupId("io.micronaut.sql")
.artifactId("micronaut-hibernate-jpa")
addDependencies(generatorContext);
}

protected void addDependencies(GeneratorContext generatorContext) {
generatorContext.addDependency(MicronautDependencyUtils.sqlDependency()
.artifactId(ARTIFACT_ID_MICRONAUT_HIBERNATE_JPA)
.compile());
generatorContext.addDependency(MicronautDependencyUtils.dataDependency()
.artifactId(MicronautDependencyUtils.ARTIFACT_ID_MICRONAUT_DATA_TX_HIBERNATE)
.compile());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ class GradleSpec extends BeanContextSpec implements CommandOutputFixture {

void 'disable Gradle Toolchain by default (dsl = #dsl)'() {
when:
def output = generate(ApplicationType.DEFAULT, new Options(Language.JAVA, dsl))
def buildGradle = output[fileName]
Map<String, String> output = generate(ApplicationType.DEFAULT, new Options(Language.JAVA, dsl))
String buildGradle = output[fileName]

then:
buildGradle
Expand All @@ -97,7 +97,14 @@ class GradleSpec extends BeanContextSpec implements CommandOutputFixture {
dsl | fileName | configuration
BuildTool.GRADLE | 'build.gradle' | 'graalvmNative.toolchainDetection = false'
BuildTool.GRADLE_KOTLIN | 'build.gradle.kts' | 'graalvmNative.toolchainDetection.set(false)'
}

void 'ignoredAutomaticDependencies not output by default'() {
when:
Map<String, String> output = generate([])

then:
!output["build.gradle.kts"].contains('ignoredAutomaticDependencies')
}

void 'disable Gradle Toolchain by default for Oracle function (dsl = #dsl)'() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import io.micronaut.starter.build.dependencies.Scope
import io.micronaut.starter.feature.database.jdbc.Hikari
import io.micronaut.starter.options.BuildTool

import static io.micronaut.starter.build.dependencies.MicronautDependencyUtils.ARTIFACT_ID_MICRONAUT_DATA_PROCESSOR_ARTIFACT
import static io.micronaut.starter.build.dependencies.MicronautDependencyUtils.GROUP_ID_MICRONAUT_DATA
import static io.micronaut.starter.build.dependencies.MicronautDependencyUtils.GROUP_ID_MICRONAUT_SQL
import static io.micronaut.starter.feature.database.DataFeature.MICRONAUT_DATA_PROCESSOR_ARTIFACT

abstract class BaseHibernateReactiveSpec extends ApplicationContextSpec {

boolean containsDataProcessor(String template) {
mavenVerifier(template).hasDependency(GROUP_ID_MICRONAUT_DATA, MICRONAUT_DATA_PROCESSOR_ARTIFACT, Scope.ANNOTATION_PROCESSOR )
mavenVerifier(template).hasDependency(GROUP_ID_MICRONAUT_DATA, ARTIFACT_ID_MICRONAUT_DATA_PROCESSOR_ARTIFACT, Scope.ANNOTATION_PROCESSOR )
}

boolean containsDataHibernateJpa(String template) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.micronaut.starter.feature.database

import groovy.xml.XmlSlurper
import io.micronaut.core.version.SemanticVersion
import io.micronaut.starter.BuildBuilder
import io.micronaut.starter.application.generator.GeneratorContext
import io.micronaut.starter.build.BuildTestUtil
Expand All @@ -18,8 +17,8 @@ import io.micronaut.starter.options.MicronautJdkVersionConfiguration
import spock.lang.Issue
import spock.lang.Requires

import static io.micronaut.starter.build.dependencies.MicronautDependencyUtils.ARTIFACT_ID_MICRONAUT_DATA_PROCESSOR_ARTIFACT
import static io.micronaut.starter.build.dependencies.MicronautDependencyUtils.GROUP_ID_MICRONAUT_DATA
import static io.micronaut.starter.feature.database.DataFeature.MICRONAUT_DATA_PROCESSOR_ARTIFACT

@Requires({ jvm.current.isJava11Compatible() })
class DataHibernateReactiveSpec extends BaseHibernateReactiveSpec {
Expand Down Expand Up @@ -343,6 +342,6 @@ class DataHibernateReactiveSpec extends BaseHibernateReactiveSpec {
BuildTestVerifier verifier = BuildTestUtil.verifier(BuildTool.MAVEN, template)

then:
verifier.hasDependency(GROUP_ID_MICRONAUT_DATA, MICRONAUT_DATA_PROCESSOR_ARTIFACT, Scope.COMPILE_ONLY)
verifier.hasDependency(GROUP_ID_MICRONAUT_DATA, ARTIFACT_ID_MICRONAUT_DATA_PROCESSOR_ARTIFACT, Scope.COMPILE_ONLY)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import io.micronaut.starter.options.Language
import io.micronaut.starter.options.Options
import spock.lang.Issue

import static io.micronaut.starter.build.dependencies.MicronautDependencyUtils.ARTIFACT_ID_MICRONAUT_DATA_PROCESSOR_ARTIFACT
import static io.micronaut.starter.build.dependencies.MicronautDependencyUtils.GROUP_ID_MICRONAUT_DATA
import static io.micronaut.starter.build.dependencies.MicronautDependencyUtils.GROUP_ID_MICRONAUT_SQL
import static io.micronaut.starter.feature.database.DataFeature.MICRONAUT_DATA_PROCESSOR_ARTIFACT
import static io.micronaut.starter.feature.database.DataJdbc.MICRONAUT_DATA_JDBC_ARTIFACT
import static io.micronaut.starter.feature.database.jdbc.Hikari.MICRONAUT_JDBC_HIKARI_ARTIFACT

Expand Down Expand Up @@ -65,7 +65,7 @@ class DataJdbcSpec extends ApplicationContextSpec implements CommandOutputFixtu
BuildTestVerifier verifier = BuildTestUtil.verifier(BuildTool.MAVEN, template)

then:
verifier.hasDependency(GROUP_ID_MICRONAUT_DATA, MICRONAUT_DATA_PROCESSOR_ARTIFACT, Scope.ANNOTATION_PROCESSOR )
verifier.hasDependency(GROUP_ID_MICRONAUT_DATA, ARTIFACT_ID_MICRONAUT_DATA_PROCESSOR_ARTIFACT, Scope.ANNOTATION_PROCESSOR )
verifier.hasDependency(GROUP_ID_MICRONAUT_DATA, MICRONAUT_DATA_JDBC_ARTIFACT, Scope.COMPILE )
verifier.hasDependency(GROUP_ID_MICRONAUT_SQL, MICRONAUT_JDBC_HIKARI_ARTIFACT, Scope.COMPILE )
verifier.hasDependency("com.h2database", "h2", Scope.RUNTIME )
Expand Down Expand Up @@ -141,6 +141,6 @@ class DataJdbcSpec extends ApplicationContextSpec implements CommandOutputFixtu

then:

verifier.hasDependency(GROUP_ID_MICRONAUT_DATA, MICRONAUT_DATA_PROCESSOR_ARTIFACT, Scope.COMPILE_ONLY )
verifier.hasDependency(GROUP_ID_MICRONAUT_DATA, ARTIFACT_ID_MICRONAUT_DATA_PROCESSOR_ARTIFACT, Scope.COMPILE_ONLY )
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import spock.lang.Issue

import static io.micronaut.starter.build.dependencies.MicronautDependencyUtils.GROUP_ID_MICRONAUT_DATA
import static io.micronaut.starter.build.dependencies.MicronautDependencyUtils.GROUP_ID_MICRONAUT_SQL
import static io.micronaut.starter.feature.database.DataFeature.MICRONAUT_DATA_PROCESSOR_ARTIFACT
import static io.micronaut.starter.build.dependencies.MicronautDependencyUtils.ARTIFACT_ID_MICRONAUT_DATA_PROCESSOR_ARTIFACT
import static io.micronaut.starter.feature.database.jdbc.Hikari.MICRONAUT_JDBC_HIKARI_ARTIFACT

class DataJpaSpec extends ApplicationContextSpec implements CommandOutputFixture {
Expand Down Expand Up @@ -66,7 +66,7 @@ class DataJpaSpec extends ApplicationContextSpec implements CommandOutputFixture
BuildTestVerifier verifier = BuildTestUtil.verifier(BuildTool.MAVEN, template)
then:
//src/main
verifier.hasDependency(GROUP_ID_MICRONAUT_DATA, MICRONAUT_DATA_PROCESSOR_ARTIFACT, Scope.ANNOTATION_PROCESSOR )
verifier.hasDependency(GROUP_ID_MICRONAUT_DATA, ARTIFACT_ID_MICRONAUT_DATA_PROCESSOR_ARTIFACT, Scope.ANNOTATION_PROCESSOR )
verifier.hasDependency(GROUP_ID_MICRONAUT_DATA, "micronaut-data-hibernate-jpa", Scope.COMPILE )
verifier.hasDependency(GROUP_ID_MICRONAUT_SQL, MICRONAUT_JDBC_HIKARI_ARTIFACT, Scope.COMPILE )
verifier.hasDependency("com.h2database", "h2", Scope.RUNTIME )
Expand Down
Loading
Loading