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

[incubator-kie-issues#1068] Fix gradle build output #3471

Merged
merged 12 commits into from
Apr 17, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Map;
import java.util.stream.Stream;

import org.drools.codegen.common.AppPaths;
import org.drools.io.FileSystemResource;
import org.drools.scenariosimulation.api.model.ScenarioSimulationModel;
import org.drools.scenariosimulation.api.model.ScesimModelDescriptor;
Expand All @@ -53,7 +54,7 @@ public class KogitoDMNScenarioRunnerHelper extends DMNScenarioRunnerHelper {

private DMNRuntime dmnRuntime = initDmnRuntime();

private static final String targetFolder = File.separator + "target" + File.separator;
private static final String targetFolder = File.separator + AppPaths.TARGET_DIR + File.separator;
private static final String generatedResourcesFolder = targetFolder + "generated-resources" + File.separator;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -258,7 +257,7 @@ protected abstract static class AbstractBuilder implements Builder {
protected Predicate<String> classAvailabilityResolver = this::hasClass;
protected Predicate<Class<?>> classSubTypeAvailabilityResolver = c -> false;
// default fallback value (usually overridden)
protected AppPaths appPaths = AppPaths.fromProjectDir(new File(".").toPath(), Path.of(".", AppPaths.TARGET_DIR));
protected AppPaths appPaths = AppPaths.fromProjectDir(new File(".").toPath());
protected KogitoGAV gav;

protected AbstractBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public static class Builder {

/**
*
* @param classesDir usually target/classes/
* @param sourcesDir usually target/generated-sources/kogito/
* @param classesDir usually generated-sources/kogito
* @param sourcesDir usually src/main/java/
* @param resourcesDir usually target/generated-resources/kogito/
* @param scaffoldedSourcesDir usually src/main/java/
* @param scaffoldedSourcesDir usually target/generated-sources/kogito/
*/
public Builder(String classesDir, String sourcesDir, String resourcesDir, String scaffoldedSourcesDir) {
this.classesDir = classesDir;
Expand All @@ -72,18 +72,12 @@ public GeneratedFileWriter build(Path basePath) {
private final Path sourcesDir;
private final Path resourcePath;
private final Path scaffoldedSourcesDir;

public static final String DEFAULT_SOURCES_DIR = "generated-sources/kogito/";
public static final String DEFAULT_RESOURCE_PATH = "generated-resources/kogito/";
public static final String DEFAULT_SCAFFOLDED_SOURCES_DIR = "src/main/java/";
public static final String DEFAULT_CLASSES_DIR = "target/classes";

/**
*
* @param classesDir usually {@link #DEFAULT_CLASSES_DIR}
* @param sourcesDir usually target/generated-sources/kogito/. See {@link #DEFAULT_SOURCES_DIR}
* @param resourcePath usually target/generated-resources/kogito/ {@link #DEFAULT_RESOURCE_PATH}
* @param scaffoldedSourcesDir usually {@link #DEFAULT_SCAFFOLDED_SOURCES_DIR}
* @param classesDir usually generated-sources/kogito
* @param sourcesDir usually src/main/java/
* @param resourcePath usually target/generated-resources/kogito/
* @param scaffoldedSourcesDir usually target/generated-sources/kogito/
*/
public GeneratedFileWriter(Path classesDir, Path sourcesDir, Path resourcePath, Path scaffoldedSourcesDir) {
this.classesDir = classesDir;
Expand All @@ -106,11 +100,7 @@ public void write(GeneratedFile f) throws UncheckedIOException {
writeGeneratedFile(f, classesDir);
break;
case SOURCE:
if (f.type().isCustomizable()) {
writeGeneratedFile(f, scaffoldedSourcesDir);
} else {
writeGeneratedFile(f, sourcesDir);
}
writeGeneratedFile(f, scaffoldedSourcesDir);
break;
default:
throw new IllegalArgumentException("Unknown Category " + category.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.NoSuchElementException;
import java.util.Optional;

import org.drools.codegen.common.AppPaths;
import org.drools.codegen.common.di.impl.CDIDependencyInjectionAnnotator;
import org.drools.util.StringUtils;
import org.junit.jupiter.api.AfterAll;
Expand Down Expand Up @@ -80,7 +81,7 @@ public static void setup() {
String filePrefix = URLEncoder.encode(getSanitizedClassName(KIE_PMML_MODEL.getFileName().replace(".pmml", "")));
String classPrefix = URLEncoder.encode(getSanitizedClassName(KIE_PMML_MODEL.getName()));
expectedUrl = String.format("/%s/%s", filePrefix, classPrefix);
System.setProperty(INDEXFILE_DIRECTORY_PROPERTY, "target/test-classes");
System.setProperty(INDEXFILE_DIRECTORY_PROPERTY, String.format("%s/test-classes", AppPaths.TARGET_DIR));
}

@AfterAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Collections;
import java.util.List;

import org.drools.codegen.common.AppPaths;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -46,7 +47,7 @@ class PredictionCodegenFactoryTest {

@BeforeAll
public static void setup() {
System.setProperty(INDEXFILE_DIRECTORY_PROPERTY, "target/test-classes");
System.setProperty(INDEXFILE_DIRECTORY_PROPERTY, String.format("%s/test-classes", AppPaths.TARGET_DIR));
}

@AfterAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.function.Function;
import java.util.stream.Stream;

import org.drools.codegen.common.AppPaths;
import org.drools.codegen.common.GeneratedFile;
import org.drools.codegen.common.GeneratedFileType;
import org.junit.jupiter.api.AfterAll;
Expand Down Expand Up @@ -68,7 +69,7 @@ class PredictionCodegenGenerateTest {

@BeforeAll
public static void setup() {
System.setProperty(INDEXFILE_DIRECTORY_PROPERTY, "target/test-classes");
System.setProperty(INDEXFILE_DIRECTORY_PROPERTY, String.format("%s/test-classes", AppPaths.TARGET_DIR));
}

@AfterAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.function.Function;
import java.util.stream.Stream;

import org.drools.codegen.common.AppPaths;
import org.drools.codegen.common.GeneratedFile;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -51,7 +52,7 @@ class PredictionCodegenInternalGenerateTest {

@BeforeAll
public static void setup() {
System.setProperty(INDEXFILE_DIRECTORY_PROPERTY, "target/test-classes");
System.setProperty(INDEXFILE_DIRECTORY_PROPERTY, String.format("%s/test-classes", AppPaths.TARGET_DIR));
}

@AfterAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.lang.reflect.Modifier;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -67,11 +68,8 @@ public abstract class AbstractKieMojo extends AbstractMojo {
@Parameter(required = true, defaultValue = "${project.build.outputDirectory}")
protected File outputDirectory;

@Parameter(defaultValue = "${project.build.directory}/" + GeneratedFileWriter.DEFAULT_SOURCES_DIR)
protected File generatedSources;

@Parameter(defaultValue = "${project.build.directory}/" + GeneratedFileWriter.DEFAULT_RESOURCE_PATH)
protected File generatedResources;
@Parameter(required = true, defaultValue = "${project.basedir}")
protected File baseDir;

@Parameter(property = "kogito.codegen.persistence", defaultValue = "true")
protected boolean persistence;
Expand Down Expand Up @@ -102,7 +100,7 @@ protected void setSystemProperties(Map<String, String> properties) {
}

protected KogitoBuildContext discoverKogitoRuntimeContext(ClassLoader classLoader) {
AppPaths appPaths = AppPaths.fromProjectDir(projectDir.toPath(), outputDirectory.toPath());
AppPaths appPaths = AppPaths.fromProjectDir(projectDir.toPath());
KogitoBuildContext context = contextBuilder()
.withClassAvailabilityResolver(this::hasClassOnClasspath)
.withClassSubTypeAvailabilityResolver(classSubTypeAvailabilityResolver())
Expand Down Expand Up @@ -250,20 +248,29 @@ private boolean hasClassOnClasspath(String className) {
}

protected void writeGeneratedFiles(Collection<GeneratedFile> generatedFiles) {
generatedFiles.forEach(this::writeGeneratedFile);
GeneratedFileWriter writer = getGeneratedFileWriter();
generatedFiles.forEach(generatedFile -> writeGeneratedFile(generatedFile, writer));
}

protected void writeGeneratedFile(GeneratedFile generatedFile) {
GeneratedFileWriter writer = new GeneratedFileWriter(outputDirectory.toPath(),
generatedSources.toPath(),
generatedResources.toPath(),
getSourcesPath().toPath());
writeGeneratedFile(generatedFile, getGeneratedFileWriter());
}

protected void writeGeneratedFile(GeneratedFile generatedFile, GeneratedFileWriter writer) {
getLog().info("Generating: " + generatedFile.relativePath());
writer.write(generatedFile);
}

protected File getSourcesPath() {
return generatedSources;
return AppPaths.BuildTool.MAVEN.GENERATED_SOURCES_PATH.toFile();
}

private GeneratedFileWriter getGeneratedFileWriter() {
Path sourcesDir = Path.of(baseDir.getAbsolutePath(), AppPaths.BuildTool.MAVEN.GENERATED_SOURCES_PATH.toString());
Path resourcesDir = Path.of(baseDir.getAbsolutePath(), AppPaths.BuildTool.MAVEN.GENERATED_RESOURCES_PATH.toString());
return new GeneratedFileWriter(outputDirectory.toPath(),
sourcesDir,
resourcesDir,
getSourcesPath().toPath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.drools.codegen.common.AppPaths;
import org.drools.codegen.common.GeneratedFile;
import org.drools.codegen.common.GeneratedFileType;
import org.kie.kogito.codegen.core.ApplicationGenerator;
Expand Down Expand Up @@ -110,7 +111,7 @@ protected File getSourcesPath() {

protected void addCompileSourceRoots() {
project.addCompileSourceRoot(getSourcesPath().getPath());
project.addCompileSourceRoot(generatedSources.getPath());
project.addCompileSourceRoot(AppPaths.BuildTool.MAVEN.GENERATED_SOURCES_PATH.toString());
}

protected void generateModel() throws MojoExecutionException {
Expand All @@ -129,9 +130,11 @@ protected void generateModel() throws MojoExecutionException {

Map<GeneratedFileType, List<GeneratedFile>> mappedGeneratedFiles = generatedFiles.stream()
.collect(Collectors.groupingBy(GeneratedFile::type));
mappedGeneratedFiles.entrySet().stream()
List<GeneratedFile> generatedUncompiledFiles = mappedGeneratedFiles.entrySet().stream()
.filter(entry -> !entry.getKey().equals(COMPILED_CLASS))
.forEach(entry -> writeGeneratedFiles(entry.getValue()));
.flatMap(entry -> entry.getValue().stream())
.toList();
writeGeneratedFiles(generatedUncompiledFiles);

List<GeneratedFile> generatedCompiledFiles = mappedGeneratedFiles.getOrDefault(COMPILED_CLASS,
Collections.emptyList())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ public void execute() throws MojoExecutionException {
compileAndWriteClasses(generatedClasses, classLoader, settings);

// Dump resources
generatedResources.forEach(this::writeGeneratedFile);
this.writeGeneratedFiles(generatedResources);

// Json schema generation
Stream<Class<?>> processClassStream = getReflections().getTypesAnnotatedWith(ProcessInput.class).stream();
generateJsonSchema(processClassStream).forEach(this::writeGeneratedFile);
writeGeneratedFiles(generateJsonSchema(processClassStream));

Stream<Class<?>> userTaskClassStream = getReflections().getTypesAnnotatedWith(UserTask.class).stream();
generateJsonSchema(userTaskClassStream).forEach(this::writeGeneratedFile);
writeGeneratedFiles(generateJsonSchema(userTaskClassStream));
} catch (Exception e) {
throw new MojoExecutionException("Error during processing model classes", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.drools.codegen.common.AppPaths;
import org.drools.codegen.common.DroolsModelBuildContext;
import org.drools.codegen.common.GeneratedFile;
import org.drools.codegen.common.GeneratedFileType;
Expand Down Expand Up @@ -107,10 +108,9 @@ public class KogitoAssetsProcessor {
@BuildStep
public KogitoBuildContextBuildItem generateKogitoBuildContext(List<KogitoBuildContextAttributeBuildItem> attributes) {
// configure the application generator
PathCollection rootPaths = getRootPaths(root.getResolvedPaths());
PathCollection rootPaths = getRootPaths(root.getResolvedPaths(), AppPaths.BT);
KogitoBuildContext context =
kogitoBuildContext(outputTargetBuildItem.getOutputDirectory(),
rootPaths,
kogitoBuildContext(rootPaths,
combinedIndexBuildItem.getIndex(),
curateOutcomeBuildItem.getApplicationModel().getAppArtifact());
attributes.forEach(attribute -> context.addContextAttribute(attribute.getName(), attribute.getValue()));
Expand Down Expand Up @@ -218,26 +218,28 @@ public EfestoGeneratedClassBuildItem reflectiveEfestoGeneratedClassBuildItem(Kog
return new EfestoGeneratedClassBuildItem(kogitoGeneratedSourcesBuildItem.getGeneratedFiles());
}

static PathCollection getRootPaths(PathCollection resolvedPaths) {
AtomicReference<PathCollection> toReturnRef = new AtomicReference<>(resolvedPaths);
if (resolvedPaths.stream().noneMatch(path -> path.endsWith(File.separator + "generated-resources"))) {
Optional<Path> optClassesPath =
resolvedPaths.stream().filter(path -> {
String fullPath = path.toString();
String dir = fullPath.substring(fullPath.lastIndexOf(File.separator) + 1);
return dir.equals("classes");
}).findFirst();
optClassesPath.ifPresent(classesPath -> {
Path toAdd = Path.of(classesPath.toString().replace(File.separator + "classes",
File.separator +
"generated" +
"-resources"));
List<Path> prevPaths = toReturnRef.get().stream().collect(Collectors.toList());
prevPaths.add(toAdd);
toReturnRef.set(PathList.from(prevPaths));
});
static PathCollection getRootPaths(PathCollection resolvedPaths, AppPaths.BuildTool bt) {
// Needed hack because during MAVEN build, resolvedPaths point to root of project,
// while during GRADLE build, resolved paths contains {root_project}/build/classes/java/main and
// {root_project}/build/resources/main
switch (bt) {
case GRADLE -> {
AtomicReference<PathCollection> toReturnRef = new AtomicReference<>(resolvedPaths);
Optional<Path> optClassesPath =
resolvedPaths.stream().filter(path -> {
String fullPath = path.toString();
String lookingFor = "build/classes/java/main".replace("/", File.separator);
return fullPath.endsWith(lookingFor);
}).findFirst();
optClassesPath.ifPresent(classesPath -> {
toReturnRef.set(PathList.of(classesPath));
});
return toReturnRef.get();
}
default -> {
return resolvedPaths;
}
}
return toReturnRef.get();
}

private Collection<GeneratedFile> collectGeneratedFiles(KogitoGeneratedSourcesBuildItem sources, List<KogitoAddonsPreGeneratedSourcesBuildItem> preSources,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
import io.quarkus.maven.dependency.Dependency;
import io.quarkus.vertx.http.deployment.spi.AdditionalStaticResourceBuildItem;

import static org.drools.util.Config.getConfig;

/**
* Utility class to aggregate and share resource handling in Kogito extensions
*/
Expand All @@ -76,16 +78,22 @@ private KogitoQuarkusResourceUtils() {

// since quarkus-maven-plugin is later phase of maven-resources-plugin,
// need to manually late-provide the resource in the expected location for quarkus:dev phase --so not: writeGeneratedFile( f, resourcePath )
private static final GeneratedFileWriter.Builder generatedFileWriterBuilder =
new GeneratedFileWriter.Builder(
"target/classes",
System.getProperty("kogito.codegen.sources.directory", "target/generated-sources/kogito/"),
System.getProperty("kogito.codegen.resources.directory", "target/generated-resources/kogito/"),
"target/generated-sources/kogito/");

public static KogitoBuildContext kogitoBuildContext(Path outputTarget, Iterable<Path> paths, IndexView index, Dependency appArtifact) {
private static final GeneratedFileWriter.Builder generatedFileWriterBuilder;

static {
String targetClasses = AppPaths.BT.CLASSES_PATH.toString();
String generatedSourcesKogito = Path.of(AppPaths.GENERATED_SOURCES_DIR, "kogito").toString();
String generatedResourcesSourcesKogito = Path.of(AppPaths.GENERATED_RESOURCES_DIR, "kogito").toString();
generatedFileWriterBuilder = new GeneratedFileWriter.Builder(
targetClasses,
getConfig("kogito.codegen.sources.directory", generatedSourcesKogito),
getConfig("kogito.codegen.resources.directory", generatedResourcesSourcesKogito),
generatedSourcesKogito);
}

public static KogitoBuildContext kogitoBuildContext(Iterable<Path> paths, IndexView index, Dependency appArtifact) {
// scan and parse paths
AppPaths appPaths = QuarkusAppPaths.from(outputTarget, paths, AppPaths.BuildTool.findBuildTool());
AppPaths appPaths = QuarkusAppPaths.from(paths, AppPaths.BT);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
KogitoBuildContext context = QuarkusKogitoBuildContext.builder()
.withApplicationPropertyProvider(new KogitoQuarkusApplicationPropertiesProvider())
Expand Down
Loading
Loading