This is an automated email from the ASF dual-hosted git repository.
gitgabrio pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-kie-drools.git
The following commit(s) were added to refs/heads/main by this push:
new faf8ac4363 [incubator-kie-issues#1068] Fix gradle build output (#5839)
faf8ac4363 is described below
commit faf8ac4363a87e7d4ceacce016e5c90f3665dc81
Author: Gabriele Cardosi <[email protected]>
AuthorDate: Wed Apr 17 10:33:50 2024 +0200
[incubator-kie-issues#1068] Fix gradle build output (#5839)
* [test-gradle-target] WIP
* [incubator-kie-issues#1068] Enhancing dual build-system management.
Locally working
* [incubator-kie-issues#1068] Remove duplicated GeneratedFileWriter
* [incubator-kie-issues#1068] Extend test coverage
* [incubator-kie-issues#1068] Remove unused parameter
* [incubator-kie-issues#1068] Fix missing/wrong headers
* [incubator-kie-issues#1068] Fix wrong reference in javadoc
---------
Co-authored-by: Gabriele-Cardosi <[email protected]>
---
drools-model/drools-codegen-common/pom.xml | 9 ++
.../java/org/drools/codegen/common/AppPaths.java | 109 ++++++++++++++-------
.../drools/codegen/common/GeneratedFileType.java | 2 +-
.../codegen/common}/GeneratedFileWriter.java | 89 ++++++++++-------
.../context/AbstractDroolsModelBuildContext.java | 2 +-
.../org/drools/codegen/common/AppPathsTest.java | 105 ++++++++++----------
.../codegen/common/GeneratedFileWriterTest.java | 99 +++++++++++++++++++
.../quarkus/deployment/DroolsAssetsProcessor.java | 5 +-
.../deployment/DroolsQuarkusResourceUtils.java | 18 ++--
.../quarkus/util/deployment/QuarkusAppPaths.java | 34 ++++---
10 files changed, 321 insertions(+), 151 deletions(-)
diff --git a/drools-model/drools-codegen-common/pom.xml
b/drools-model/drools-codegen-common/pom.xml
index a0888da917..d8d7227d22 100644
--- a/drools-model/drools-codegen-common/pom.xml
+++ b/drools-model/drools-codegen-common/pom.xml
@@ -39,6 +39,10 @@
</properties>
<dependencies>
+ <dependency>
+ <groupId>org.drools</groupId>
+ <artifactId>drools-util</artifactId>
+ </dependency>
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
@@ -58,5 +62,10 @@
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
\ No newline at end of file
diff --git
a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/AppPaths.java
b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/AppPaths.java
index e12fd0c550..a03639b7b0 100644
---
a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/AppPaths.java
+++
b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/AppPaths.java
@@ -26,28 +26,63 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
+import java.util.List;
import java.util.Set;
import java.util.function.UnaryOperator;
public class AppPaths {
public enum BuildTool {
- MAVEN,
- GRADLE;
+ MAVEN("target",
+ Path.of("target","generated-sources"),
+ Path.of("target","generated-resources"),
+ Path.of("target","generated-test-resources"),
+ Path.of("target","classes"),
+ Path.of("target","test-classes")),
+ GRADLE("build",
+ Path.of("build", "generated", "sources"),
+ Path.of("build","generated", "resources"),
+ Path.of("build","generated", "test", "resources"),
+ Path.of("build","classes", "java", "main"),
+ Path.of("build","classes", "java", "test"));
+
+ public final String OUTPUT_DIRECTORY;
+ public final Path GENERATED_SOURCES_PATH;
+ public final Path GENERATED_RESOURCES_PATH;
+ public final Path GENERATED_TEST_RESOURCES_PATH;
+ public final Path CLASSES_PATH;
+ public final Path TEST_CLASSES_PATH;
+
+ BuildTool(String outputDirectory, Path generatedSourcesPath, Path
generatedResourcesPath, Path generatedTestResourcesPath, Path classesPath, Path
testClassesPath) {
+ this.OUTPUT_DIRECTORY = outputDirectory;
+ this.GENERATED_SOURCES_PATH = generatedSourcesPath;
+ this.GENERATED_RESOURCES_PATH = generatedResourcesPath;
+ this.GENERATED_TEST_RESOURCES_PATH = generatedTestResourcesPath;
+ this.CLASSES_PATH = classesPath;
+ this.TEST_CLASSES_PATH = testClassesPath;
+ }
public static AppPaths.BuildTool findBuildTool() {
return System.getProperty("org.gradle.appname") == null ? MAVEN :
GRADLE;
}
}
- public static final String TARGET_DIR = "target";
+ public static final String TARGET_DIR;
+ public static final String GENERATED_SOURCES_DIR;
+ public static final String GENERATED_RESOURCES_DIR;
+ public static final BuildTool BT;
+
+ static {
+ BT = BuildTool.findBuildTool();
+ TARGET_DIR = BT.OUTPUT_DIRECTORY;
+ GENERATED_SOURCES_DIR = BT.GENERATED_SOURCES_PATH.toString();
+ GENERATED_RESOURCES_DIR = BT.GENERATED_RESOURCES_PATH.toString();
+ }
public static final String SRC_DIR = "src";
public static final String RESOURCES_DIR = "resources";
- public static final String GENERATED_RESOURCES_DIR = "generated-resources";
-
public static final String MAIN_DIR = "main";
public static final String TEST_DIR = "test";
@@ -66,8 +101,8 @@ public class AppPaths {
private final Path[] sourcePaths;
- public static AppPaths fromProjectDir(Path projectDir, Path outputTarget) {
- return new AppPaths(Collections.singleton(projectDir),
Collections.emptyList(), false, BuildTool.findBuildTool(), MAIN_DIR,
outputTarget);
+ public static AppPaths fromProjectDir(Path projectDir) {
+ return fromProjectDir(projectDir, BT);
}
/**
@@ -77,7 +112,27 @@ public class AppPaths {
* @return
*/
public static AppPaths fromTestDir(Path projectDir) {
- return new AppPaths(Collections.singleton(projectDir),
Collections.emptyList(), false, BuildTool.findBuildTool(), TEST_DIR,
Paths.get(projectDir.toString(), TARGET_DIR));
+ return fromTestDir(projectDir, BT);
+ }
+
+ /**
+ * Default-access method for testing purpose
+ * @param projectDir
+ * @param bt
+ * @return
+ */
+ static AppPaths fromProjectDir(Path projectDir, BuildTool bt) {
+ return new AppPaths(Collections.singletonList(projectDir),
Collections.emptyList(), false, bt, MAIN_DIR, false);
+ }
+
+ /**
+ * Default-access method for testing purpose
+ * @param projectDir
+ * @param bt
+ * @return
+ */
+ static AppPaths fromTestDir(Path projectDir, BuildTool bt) {
+ return new AppPaths(Collections.singletonList(projectDir),
Collections.emptyList(), false, bt, TEST_DIR, true);
}
/**
@@ -87,14 +142,14 @@ public class AppPaths {
* @param bt
* @param resourcesBasePath "main" or "test"
*/
- protected AppPaths(Set<Path> projectPaths, Collection<Path> classesPaths,
boolean isJar, BuildTool bt,
- String resourcesBasePath, Path outputTarget) {
+ protected AppPaths(List<Path> projectPaths, Collection<Path> classesPaths,
boolean isJar, BuildTool bt,
+ String resourcesBasePath, boolean isTest) {
this.isJar = isJar;
this.projectPaths.addAll(projectPaths);
this.classesPaths.addAll(classesPaths);
- this.outputTarget = outputTarget;
- firstProjectPath = getFirstProjectPath(this.projectPaths,
outputTarget, bt);
- resourcePaths = getResourcePaths(this.projectPaths, resourcesBasePath,
bt);
+ this.outputTarget = Paths.get(".", bt.OUTPUT_DIRECTORY);
+ firstProjectPath = projectPaths.get(0);
+ resourcePaths = getResourcePaths(this.projectPaths, resourcesBasePath,
bt, isTest);
paths = isJar ? getJarPaths(isJar, this.classesPaths) : resourcePaths;
resourceFiles = getResourceFiles(resourcePaths);
sourcePaths = getSourcePaths(this.projectPaths);
@@ -137,12 +192,6 @@ public class AppPaths {
'}';
}
- static Path getFirstProjectPath(Set<Path> innerProjectPaths, Path
innerOutputTarget, BuildTool innerBt) {
- return innerBt == BuildTool.MAVEN
- ? innerProjectPaths.iterator().next()
- : innerOutputTarget;
- }
-
static Path[] getJarPaths(boolean isInnerJar, Collection<Path>
innerClassesPaths) {
if (!isInnerJar) {
throw new IllegalStateException("Not a jar");
@@ -151,20 +200,14 @@ public class AppPaths {
}
}
- static Path[] getResourcePaths(Set<Path> innerProjectPaths, String
resourcesBasePath, BuildTool innerBt) {
- Path[] toReturn;
- if (innerBt == BuildTool.GRADLE) {
- toReturn = transformPaths(innerProjectPaths, p ->
p.resolve(Paths.get("")));
- } else {
- toReturn = transformPaths(innerProjectPaths, p ->
p.resolve(Paths.get(SRC_DIR, resourcesBasePath,
-
RESOURCES_DIR)));
- Path[] generatedResourcesPaths = transformPaths(innerProjectPaths,
p -> p.resolve(Paths.get(TARGET_DIR,
-
GENERATED_RESOURCES_DIR)));
- Path[] newToReturn = new Path[toReturn.length +
generatedResourcesPaths.length];
- System.arraycopy(toReturn, 0, newToReturn, 0, toReturn.length);
- System.arraycopy(generatedResourcesPaths, 0, newToReturn,
toReturn.length, generatedResourcesPaths.length);
- toReturn = newToReturn;
- }
+ static Path[] getResourcePaths(Set<Path> innerProjectPaths, String
resourcesBasePath, BuildTool innerBt, boolean isTest) {
+ Path[] resourcesPaths = transformPaths(innerProjectPaths, p ->
p.resolve(Paths.get(SRC_DIR, resourcesBasePath,
+
RESOURCES_DIR)));
+ Path generatedResourcesPath = isTest ?
innerBt.GENERATED_TEST_RESOURCES_PATH : innerBt.GENERATED_RESOURCES_PATH;
+ Path[] generatedResourcesPaths = transformPaths(innerProjectPaths, p
-> p.resolve(generatedResourcesPath));
+ Path[] toReturn = new Path[resourcesPaths.length +
generatedResourcesPaths.length];
+ System.arraycopy(resourcesPaths, 0, toReturn, 0,
resourcesPaths.length);
+ System.arraycopy(generatedResourcesPaths, 0, toReturn,
resourcesPaths.length, generatedResourcesPaths.length);
return toReturn;
}
diff --git
a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/GeneratedFileType.java
b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/GeneratedFileType.java
index 8818f461f8..9af06e1414 100644
---
a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/GeneratedFileType.java
+++
b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/GeneratedFileType.java
@@ -57,7 +57,7 @@ public interface GeneratedFileType {
/**
* Represent a cp resource automatically generated during codegen, so
after generate-resources maven phase.
* This means to add it to target/classes both for Quarkus or using
kogito-maven-plugin (SB). For additional
- * information see {@link
org.kie.kogito.codegen.utils.GeneratedFileWriter#write(GeneratedFile)}
+ * information see {@link
org.drools.codegen.common.GeneratedFileWriter#write(GeneratedFile)}
* For Quarkus it will be subject of GeneratedResourceBuildItem and
NativeImageResourceBuildItem too
*/
INTERNAL_RESOURCE,
diff --git
a/drools-quarkus-extension/drools-quarkus-util-deployment/src/main/java/org/drools/quarkus/util/deployment/GeneratedFileWriter.java
b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/GeneratedFileWriter.java
similarity index 59%
rename from
drools-quarkus-extension/drools-quarkus-util-deployment/src/main/java/org/drools/quarkus/util/deployment/GeneratedFileWriter.java
rename to
drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/GeneratedFileWriter.java
index e3c62e60ea..feacd558cc 100644
---
a/drools-quarkus-extension/drools-quarkus-util-deployment/src/main/java/org/drools/quarkus/util/deployment/GeneratedFileWriter.java
+++
b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/GeneratedFileWriter.java
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.drools.quarkus.util.deployment;
+package org.drools.codegen.common;
import java.io.IOException;
import java.io.UncheckedIOException;
@@ -24,28 +24,62 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
-import org.drools.codegen.common.GeneratedFile;
-import org.drools.codegen.common.GeneratedFileType;
+import static org.drools.util.Config.getConfig;
+/**
+ * Writes {@link GeneratedFile} to the right directory, depending on its
+ * {@link GeneratedFileType.Category}
+ */
public class GeneratedFileWriter {
- public static class Builder {
+ /**
+ *
+ * @param finalPath e.g. "drools" or "kogito"
+ * @param resourcesDirectoryProperty e.g.
"drools.codegen.resources.directory" or "kogito.codegen.resources.directory"
+ * @param sourcesDirectoryProperty e.g. "drools.codegen.sources.directory"
or "kogito.codegen.sources.directory"
+ * @return
+ */
+ public static Builder builder(String finalPath, String
resourcesDirectoryProperty, String sourcesDirectoryProperty ) {
+ return builder(finalPath,
+ resourcesDirectoryProperty,
+ sourcesDirectoryProperty,
+ AppPaths.BT);
+ }
+
+ /**
+ * Default-access for testing purpose
+ * @param finalPath
+ * @param resourcesDirectoryProperty
+ * @param sourcesDirectoryProperty
+ * @param bt
+ * @return
+ */
+ static Builder builder(String finalPath, String
resourcesDirectoryProperty, String sourcesDirectoryProperty, AppPaths.BuildTool
bt ) {
+ // using runtime BT instead to allow usage of
+ // Springboot from GRADLE
+ String targetClasses = bt.CLASSES_PATH.toString();
+
+ String generatedResourcesSourcesKogito =
Path.of(bt.GENERATED_RESOURCES_PATH.toString(), finalPath).toString();
+ String generatedSourcesKogito =
Path.of(bt.GENERATED_SOURCES_PATH.toString(), finalPath).toString();
+ return new Builder(targetClasses,
+ getConfig(resourcesDirectoryProperty,
generatedResourcesSourcesKogito),
+ getConfig(sourcesDirectoryProperty,
generatedSourcesKogito));
+ }
- private final String classesDir;
- private final String sourcesDir;
- private final String resourcePath;
- private final String scaffoldedSourcesDir;
+ public static class Builder {
+ //Default-access for testing purpose
+ final String classesDir;
+ final String resourcePath;
+ final String scaffoldedSourcesDir;
/**
*
* @param classesDir usually target/classes/
- * @param sourcesDir usually target/generated-sources/kogito/
* @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) {
+ private Builder(String classesDir, String resourcesDir, String
scaffoldedSourcesDir) {
this.classesDir = classesDir;
- this.sourcesDir = sourcesDir;
this.resourcePath = resourcesDir;
this.scaffoldedSourcesDir = scaffoldedSourcesDir;
}
@@ -58,32 +92,23 @@ public class GeneratedFileWriter {
public GeneratedFileWriter build(Path basePath) {
return new GeneratedFileWriter(
basePath.resolve(classesDir),
- basePath.resolve(sourcesDir),
basePath.resolve(resourcePath),
basePath.resolve(scaffoldedSourcesDir));
}
}
private final Path classesDir;
- 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 target/classes/
+ * @param resourcePath usually target/generated-resources/kogito/
+ * @param scaffoldedSourcesDir usually target/generated-sources/kogito/
*/
- public GeneratedFileWriter(Path classesDir, Path sourcesDir, Path
resourcePath, Path scaffoldedSourcesDir) {
+ //Default-access for testing purpose
+ GeneratedFileWriter(Path classesDir, Path resourcePath, Path
scaffoldedSourcesDir) {
this.classesDir = classesDir;
- this.sourcesDir = sourcesDir;
this.resourcePath = resourcePath;
this.scaffoldedSourcesDir = scaffoldedSourcesDir;
}
@@ -102,11 +127,7 @@ public class GeneratedFileWriter {
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());
@@ -120,10 +141,6 @@ public class GeneratedFileWriter {
return classesDir;
}
- public Path getSourcesDir() {
- return sourcesDir;
- }
-
public Path getResourcePath() {
return resourcePath;
}
@@ -132,7 +149,7 @@ public class GeneratedFileWriter {
return scaffoldedSourcesDir;
}
- private void writeGeneratedFile(GeneratedFile f, Path location) throws
IOException {
+ void writeGeneratedFile(GeneratedFile f, Path location) throws IOException
{
if (location == null) {
return;
}
diff --git
a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/context/AbstractDroolsModelBuildContext.java
b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/context/AbstractDroolsModelBuildContext.java
index 487f5f033a..eb732b40eb 100644
---
a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/context/AbstractDroolsModelBuildContext.java
+++
b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/context/AbstractDroolsModelBuildContext.java
@@ -153,7 +153,7 @@ public abstract class AbstractDroolsModelBuildContext
implements DroolsModelBuil
protected ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
protected Predicate<String> classAvailabilityResolver = this::hasClass;
// default fallback value (usually overridden)
- protected AppPaths appPaths = AppPaths.fromProjectDir(new
File(".").toPath(), Paths.get(".", AppPaths.TARGET_DIR));
+ protected AppPaths appPaths = AppPaths.fromProjectDir(new
File(".").toPath());
protected AbstractBuilder() {
}
diff --git
a/drools-model/drools-codegen-common/src/test/java/org/drools/codegen/common/AppPathsTest.java
b/drools-model/drools-codegen-common/src/test/java/org/drools/codegen/common/AppPathsTest.java
index ed7824518b..74cf5a75b5 100644
---
a/drools-model/drools-codegen-common/src/test/java/org/drools/codegen/common/AppPathsTest.java
+++
b/drools-model/drools-codegen-common/src/test/java/org/drools/codegen/common/AppPathsTest.java
@@ -7,13 +7,11 @@ import java.util.Collection;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.ValueSource;
+import org.junit.jupiter.params.provider.EnumSource;
-import static org.drools.codegen.common.AppPaths.GENERATED_RESOURCES_DIR;
import static org.drools.codegen.common.AppPaths.MAIN_DIR;
import static org.drools.codegen.common.AppPaths.RESOURCES_DIR;
import static org.drools.codegen.common.AppPaths.SRC_DIR;
-import static org.drools.codegen.common.AppPaths.TARGET_DIR;
import static org.drools.codegen.common.AppPaths.TEST_DIR;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -22,21 +20,28 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
class AppPathsTest {
@ParameterizedTest
- @ValueSource(booleans = {true, false})
- void fromProjectDir(boolean withGradle) {
+ @EnumSource(value = AppPaths.BuildTool.class, names = {"GRADLE", "MAVEN"})
+ void fromProjectDir(AppPaths.BuildTool bt) {
String projectDirPath = "projectDir";
- String outputTargetPath = "outputTarget";
+ String outputTargetPath;
+ String generatedResourceDir;
Path projectDir = Path.of(projectDirPath);
- Path outputTarget = Path.of(outputTargetPath);
- if (withGradle) {
- System.setProperty("org.gradle.appname", "gradle-impl");
- } else {
- System.clearProperty("org.gradle.appname");
- }
- AppPaths retrieved = AppPaths.fromProjectDir(projectDir, outputTarget);
+ boolean withGradle;
+ generatedResourceDir = switch (bt) {
+ case GRADLE -> {
+ withGradle = true;
+ yield "generated/resources";
+ }
+ default -> {
+ withGradle = false;
+ yield "generated-resources";
+ }
+ };
+ outputTargetPath = bt.OUTPUT_DIRECTORY;
+ AppPaths retrieved = AppPaths.fromProjectDir(projectDir, bt);
getPathsTest(retrieved, projectDirPath, withGradle, false);
- getFirstProjectPathTest(retrieved, projectDirPath, outputTargetPath,
withGradle);
- getResourceFilesTest(retrieved, projectDirPath, withGradle, false);
+ getFirstProjectPathTest(retrieved, projectDirPath);
+ getResourceFilesTest(retrieved, projectDirPath, outputTargetPath,
generatedResourceDir, false);
getResourcePathsTest(retrieved, projectDirPath, withGradle, false);
getSourcePathsTest(retrieved, projectDirPath);
getClassesPathsTest(retrieved);
@@ -44,20 +49,28 @@ class AppPathsTest {
}
@ParameterizedTest
- @ValueSource(booleans = {true, false})
- void fromTestDir(boolean withGradle) {
+ @EnumSource(value = AppPaths.BuildTool.class, names = {"GRADLE", "MAVEN"})
+ void fromTestDir(AppPaths.BuildTool bt) {
String projectDirPath = "projectDir";
- String outputTargetPath = String.format("%s/%s", projectDirPath,
TARGET_DIR).replace("/", File.separator);
+ String outputTargetPath;
+ String generatedResourceDir;
Path projectDir = Path.of(projectDirPath);
- if (withGradle) {
- System.setProperty("org.gradle.appname", "gradle-impl");
- } else {
- System.clearProperty("org.gradle.appname");
- }
- AppPaths retrieved = AppPaths.fromTestDir(projectDir);
+ boolean withGradle;
+ generatedResourceDir = switch (bt) {
+ case GRADLE -> {
+ withGradle = true;
+ yield "generated/test/resources";
+ }
+ default -> {
+ withGradle = false;
+ yield "generated-test-resources";
+ }
+ };
+ outputTargetPath = bt.OUTPUT_DIRECTORY;
+ AppPaths retrieved = AppPaths.fromTestDir(projectDir, bt);
getPathsTest(retrieved, projectDirPath, withGradle, true);
- getFirstProjectPathTest(retrieved, projectDirPath, outputTargetPath,
withGradle);
- getResourceFilesTest(retrieved, projectDirPath, withGradle, true);
+ getFirstProjectPathTest(retrieved, projectDirPath);
+ getResourceFilesTest(retrieved, projectDirPath, outputTargetPath,
generatedResourceDir, true);
getResourcePathsTest(retrieved, projectDirPath, withGradle, true);
getSourcePathsTest(retrieved, projectDirPath);
getClassesPathsTest(retrieved);
@@ -69,33 +82,21 @@ class AppPathsTest {
commonCheckResourcePaths(retrieved, projectDirPath, isGradle,
isTestDir,"getPathsTest");
}
- private void getFirstProjectPathTest(AppPaths toCheck, String projectPath,
String outputPath, boolean isGradle) {
+ private void getFirstProjectPathTest(AppPaths toCheck, String
expectedPath) {
Path retrieved = toCheck.getFirstProjectPath();
- String expectedPath;
- if (isGradle) {
- expectedPath = outputPath;
- } else {
- expectedPath = projectPath;
- }
assertEquals(Path.of(expectedPath), retrieved,
"AppPathsTest.getFirstProjectPathTest");
}
- private void getResourceFilesTest(AppPaths toCheck, String projectDirPath,
boolean isGradle, boolean isTestDir) {
+ private void getResourceFilesTest(AppPaths toCheck, String projectDirPath,
String outputDir, String generatedResourceDir, boolean isTestDir) {
File[] retrieved = toCheck.getResourceFiles();
- int expected = isGradle ? 1 : 2;
+ int expected = 2;
assertEquals(expected, retrieved.length,
"AppPathsTest.getResourceFilesTest");
String expectedPath;
String sourceDir = isTestDir ? TEST_DIR : MAIN_DIR;
- if (isGradle) {
- expectedPath = projectDirPath;
- } else {
- expectedPath = String.format("%s/%s/%s/%s", projectDirPath,
SRC_DIR, sourceDir, RESOURCES_DIR).replace("/", File.separator);
- }
+ expectedPath = String.format("%s/%s/%s/%s", projectDirPath, SRC_DIR,
sourceDir, RESOURCES_DIR).replace("/", File.separator);
assertEquals(new File(expectedPath), retrieved[0],
"AppPathsTest.getResourceFilesTest");
- if (!isGradle) {
- expectedPath = String.format("%s/%s/%s", projectDirPath,
TARGET_DIR, GENERATED_RESOURCES_DIR).replace("/", File.separator);
- assertEquals(new File(expectedPath), retrieved[1],
"AppPathsTest.getResourceFilesTest");
- }
+ expectedPath = String.format("%s/%s/%s", projectDirPath, outputDir,
generatedResourceDir).replace("/", File.separator);
+ assertEquals(new File(expectedPath), retrieved[1],
"AppPathsTest.getResourceFilesTest");
}
private void getResourcePathsTest(AppPaths toCheck, String projectDirPath,
boolean isGradle, boolean isTestDir) {
@@ -117,22 +118,24 @@ class AppPathsTest {
private void getOutputTargetTest(AppPaths toCheck, String outputPath) {
Path retrieved = toCheck.getOutputTarget();
+ outputPath = String.format(".%s%s", File.separator, outputPath);
assertEquals(Path.of(outputPath), retrieved,
"AppPathsTest.getOutputTargetTest");
}
private void commonCheckResourcePaths(Path[] toCheck, String
projectDirPath, boolean isGradle, boolean isTestDir, String methodName) {
- int expected = isGradle ? 1 : 2;
+ int expected = 2;
assertEquals(expected, toCheck.length,
String.format("AppPathsTest.%s", methodName));
String expectedPath;
String sourceDir = isTestDir ? "test" : "main";
+ expectedPath = String.format("%s/src/%s/resources", projectDirPath,
sourceDir).replace("/", File.separator);
+ assertEquals(Path.of(expectedPath), toCheck[0],
String.format("AppPathsTest.%s", methodName));
if (isGradle) {
- expectedPath = projectDirPath;
+ String toFormat = isTestDir ? "%s/build/generated/test/resources"
: "%s/build/generated/resources";
+ expectedPath = String.format(toFormat,
projectDirPath).replace("/", File.separator);
+ assertEquals(Path.of(expectedPath), toCheck[1],
String.format("AppPathsTest.%s", methodName));
} else {
- expectedPath = String.format("%s/src/%s/resources",
projectDirPath, sourceDir).replace("/", File.separator);
- }
- assertEquals(Path.of(expectedPath), toCheck[0],
String.format("AppPathsTest.%s", methodName));
- if (!isGradle) {
- expectedPath = String.format("%s/target/generated-resources",
projectDirPath).replace("/", File.separator);
+ String toFormat = isTestDir ? "%s/target/generated-test-resources"
: "%s/target/generated-resources";
+ expectedPath = String.format(toFormat,
projectDirPath).replace("/", File.separator);
assertEquals(Path.of(expectedPath), toCheck[1],
String.format("AppPathsTest.%s", methodName));
}
}
diff --git
a/drools-model/drools-codegen-common/src/test/java/org/drools/codegen/common/GeneratedFileWriterTest.java
b/drools-model/drools-codegen-common/src/test/java/org/drools/codegen/common/GeneratedFileWriterTest.java
new file mode 100644
index 0000000000..6cb5b1b181
--- /dev/null
+++
b/drools-model/drools-codegen-common/src/test/java/org/drools/codegen/common/GeneratedFileWriterTest.java
@@ -0,0 +1,99 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.drools.codegen.common;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.stream.Stream;
+
+import org.junit.jupiter.api.parallel.Execution;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.EnumSource;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.parallel.ExecutionMode.SAME_THREAD;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+@Execution(SAME_THREAD)
+class GeneratedFileWriterTest {
+
+ @ParameterizedTest
+ @EnumSource(value = AppPaths.BuildTool.class, names = {"GRADLE", "MAVEN"})
+ void builderWithConfig(AppPaths.BuildTool bt) {
+ String resourcesDirectoryProperty = "resource.property";
+ String sourcesDirectoryProperty = "source.property";
+ String finalPath = "final/destination/";
+ String resourcesDirectory = "custom/resources/path";
+ String sourcesDirectory = "custom/sources/path";
+ System.setProperty(resourcesDirectoryProperty, resourcesDirectory);
+ System.setProperty(sourcesDirectoryProperty, sourcesDirectory);
+ GeneratedFileWriter.Builder retrieved =
GeneratedFileWriter.builder(finalPath, resourcesDirectoryProperty,
+
sourcesDirectoryProperty, bt);
+ assertEquals(bt.CLASSES_PATH.toString(), retrieved.classesDir);
+ assertEquals(resourcesDirectory, retrieved.resourcePath);
+ assertEquals(sourcesDirectory, retrieved.scaffoldedSourcesDir);
+ System.clearProperty(resourcesDirectoryProperty);
+ System.clearProperty(sourcesDirectoryProperty);
+ }
+
+ @ParameterizedTest
+ @EnumSource(value = AppPaths.BuildTool.class, names = {"GRADLE", "MAVEN"})
+ void builderWithoutConfig(AppPaths.BuildTool bt) {
+ String resourcesDirectoryProperty = "resource.property";
+ String sourcesDirectoryProperty = "source.property";
+ String finalPath = "final";
+ GeneratedFileWriter.Builder retrieved =
GeneratedFileWriter.builder(finalPath, resourcesDirectoryProperty,
+
sourcesDirectoryProperty, bt);
+ assertEquals(bt.CLASSES_PATH.toString(), retrieved.classesDir);
+ String expected = String.format("%s/%s",
bt.GENERATED_RESOURCES_PATH.toString(), finalPath).replace("/",
+
File.separator);
+ assertEquals(expected, retrieved.resourcePath);
+ expected = String.format("%s/%s",
bt.GENERATED_SOURCES_PATH.toString(), finalPath).replace("/", File.separator);
+ assertEquals(expected, retrieved.scaffoldedSourcesDir);
+ }
+
+ @ParameterizedTest
+ @MethodSource("btAndGeneratedFileCategoryProvider")
+ void write(AppPaths.BuildTool bt, GeneratedFileType.Category category)
throws IOException {
+ String relativePath = "relative/path";
+ GeneratedFile generatedFile = new
GeneratedFile(GeneratedFileType.of(category), relativePath, "");
+ GeneratedFileWriter spiedWriter = spy(new
GeneratedFileWriter(bt.CLASSES_PATH, bt.GENERATED_RESOURCES_PATH,
+
bt.GENERATED_SOURCES_PATH));
+ spiedWriter.write(generatedFile);
+ Path location = switch (category) {
+ case INTERNAL_RESOURCE, STATIC_HTTP_RESOURCE, COMPILED_CLASS ->
bt.CLASSES_PATH;
+ case SOURCE -> bt.GENERATED_SOURCES_PATH;
+ };
+ verify(spiedWriter).writeGeneratedFile(generatedFile, location);
+ }
+
+ static Stream<Arguments> btAndGeneratedFileCategoryProvider() {
+ Stream.Builder<Arguments> argumentBuilder = Stream.builder();
+ for (AppPaths.BuildTool bt : AppPaths.BuildTool.values()) {
+ for (GeneratedFileType.Category category :
GeneratedFileType.Category.values()) {
+ argumentBuilder.add(Arguments.of(bt, category));
+ }
+ }
+ return argumentBuilder.build();
+ }
+}
\ No newline at end of file
diff --git
a/drools-quarkus-extension/drools-quarkus-deployment/src/main/java/org/drools/quarkus/deployment/DroolsAssetsProcessor.java
b/drools-quarkus-extension/drools-quarkus-deployment/src/main/java/org/drools/quarkus/deployment/DroolsAssetsProcessor.java
index 30d9a6dd35..ded98cfc90 100644
---
a/drools-quarkus-extension/drools-quarkus-deployment/src/main/java/org/drools/quarkus/deployment/DroolsAssetsProcessor.java
+++
b/drools-quarkus-extension/drools-quarkus-deployment/src/main/java/org/drools/quarkus/deployment/DroolsAssetsProcessor.java
@@ -32,7 +32,6 @@ import
io.quarkus.deployment.builditem.GeneratedResourceBuildItem;
import io.quarkus.deployment.builditem.LiveReloadBuildItem;
import
io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem;
-import io.quarkus.deployment.pkg.builditem.OutputTargetBuildItem;
import io.quarkus.maven.dependency.ResolvedDependency;
import io.quarkus.resteasy.reactive.spi.GeneratedJaxRsResourceBuildItem;
import io.quarkus.vertx.http.deployment.spi.AdditionalStaticResourceBuildItem;
@@ -71,8 +70,6 @@ public class DroolsAssetsProcessor {
CurateOutcomeBuildItem curateOutcomeBuildItem;
@Inject
CombinedIndexBuildItem combinedIndexBuildItem;
- @Inject
- OutputTargetBuildItem outputTargetBuildItem;
private static final String FEATURE = "drools";
@@ -91,7 +88,7 @@ public class DroolsAssetsProcessor {
BuildProducer<GlobalsBuildItem> globalsBI,
BuildProducer<GeneratedJaxRsResourceBuildItem> jaxrsProducer) {
DroolsModelBuildContext context =
-
createDroolsBuildContext(outputTargetBuildItem.getOutputDirectory(),
root.getPaths(), combinedIndexBuildItem.getIndex());
+ createDroolsBuildContext(root.getPaths(),
combinedIndexBuildItem.getIndex());
Collection<Resource> resources =
ResourceCollector.fromPaths(context.getAppPaths().getPaths());
diff --git
a/drools-quarkus-extension/drools-quarkus-util-deployment/src/main/java/org/drools/quarkus/util/deployment/DroolsQuarkusResourceUtils.java
b/drools-quarkus-extension/drools-quarkus-util-deployment/src/main/java/org/drools/quarkus/util/deployment/DroolsQuarkusResourceUtils.java
index 612dfeceb5..2f774a9d22 100644
---
a/drools-quarkus-extension/drools-quarkus-util-deployment/src/main/java/org/drools/quarkus/util/deployment/DroolsQuarkusResourceUtils.java
+++
b/drools-quarkus-extension/drools-quarkus-util-deployment/src/main/java/org/drools/quarkus/util/deployment/DroolsQuarkusResourceUtils.java
@@ -41,6 +41,7 @@ import org.drools.codegen.common.AppPaths;
import org.drools.codegen.common.DroolsModelBuildContext;
import org.drools.codegen.common.GeneratedFile;
import org.drools.codegen.common.GeneratedFileType;
+import org.drools.codegen.common.GeneratedFileWriter;
import org.drools.codegen.common.context.QuarkusDroolsModelBuildContext;
import org.drools.wiring.api.ComponentsSupplier;
import org.jboss.jandex.ClassInfo;
@@ -52,7 +53,6 @@ import org.kie.memorycompiler.JavaCompilerSettings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import static org.drools.util.Config.getConfig;
import static org.kie.memorycompiler.KieMemoryCompiler.compileNoLoad;
/**
@@ -79,17 +79,13 @@ public class DroolsQuarkusResourceUtils {
// 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",
- getConfig("drools.codegen.sources.directory",
"target/generated-sources/drools/"),
- getConfig("drools.codegen.resources.directory",
"target/generated-resources/drools/"),
- "target/generated-sources/drools/");
-
- public static DroolsModelBuildContext createDroolsBuildContext(Path
outputTarget, Iterable<Path> paths, IndexView index) {
+ private static final GeneratedFileWriter.Builder
generatedFileWriterBuilder = GeneratedFileWriter.builder("drools"
+ , "drools.codegen.resources.directory",
"drools.codegen.sources.directory");
+
+
+ public static DroolsModelBuildContext
createDroolsBuildContext(Iterable<Path> paths, IndexView index) {
// scan and parse paths
- AppPaths.BuildTool buildTool = AppPaths.BuildTool.findBuildTool();
- AppPaths appPaths = QuarkusAppPaths.from(outputTarget, paths,
buildTool);
+ AppPaths appPaths = QuarkusAppPaths.from(paths);
ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
DroolsModelBuildContext context =
QuarkusDroolsModelBuildContext.builder()
.withClassLoader(classLoader)
diff --git
a/drools-quarkus-extension/drools-quarkus-util-deployment/src/main/java/org/drools/quarkus/util/deployment/QuarkusAppPaths.java
b/drools-quarkus-extension/drools-quarkus-util-deployment/src/main/java/org/drools/quarkus/util/deployment/QuarkusAppPaths.java
index 47abd3ae15..2f76fa71dc 100644
---
a/drools-quarkus-extension/drools-quarkus-util-deployment/src/main/java/org/drools/quarkus/util/deployment/QuarkusAppPaths.java
+++
b/drools-quarkus-extension/drools-quarkus-util-deployment/src/main/java/org/drools/quarkus/util/deployment/QuarkusAppPaths.java
@@ -24,11 +24,11 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
+import java.util.List;
import java.util.Set;
-import org.drools.codegen.common.AppPaths;
-
import io.quarkus.deployment.pkg.steps.JarResultBuildStep;
+import org.drools.codegen.common.AppPaths;
/**
* {@link AppPaths}'s extension in Quarkus context.
@@ -36,8 +36,6 @@ import io.quarkus.deployment.pkg.steps.JarResultBuildStep;
public class QuarkusAppPaths extends AppPaths {
private static final Path MUTABLE_JAR_PATH = Paths.get("dev", "app");
- private static final Path CLASSES_PATH = Paths.get(TARGET_DIR, "classes");
- private static final Path TEST_CLASSES_PATH = Paths.get(TARGET_DIR,
"test-classes");
private enum PathType {
CLASSES,
@@ -53,11 +51,11 @@ public class QuarkusAppPaths extends AppPaths {
UNKNOWN
}
- protected QuarkusAppPaths(Set<Path> projectPaths, Collection<Path>
classesPaths, boolean isJar, BuildTool bt, Path outputTarget) {
- super(projectPaths, classesPaths, isJar, bt, JarResultBuildStep.MAIN,
outputTarget);
+ protected QuarkusAppPaths(List<Path> projectPaths, Collection<Path>
classesPaths, boolean isJar) {
+ super(projectPaths, classesPaths, isJar, AppPaths.BT,
JarResultBuildStep.MAIN, false);
}
- public static AppPaths from(Path outputTarget, Iterable<Path> paths,
AppPaths.BuildTool bt) {
+ public static AppPaths from(Iterable<Path> paths) {
final Set<Path> projectPaths = new LinkedHashSet<>();
final Collection<Path> classesPaths = new ArrayList<>();
boolean isJar = false;
@@ -66,37 +64,45 @@ public class QuarkusAppPaths extends AppPaths {
switch (pathType) {
case CLASSES:
classesPaths.add(path);
- projectPaths.add(path.getParent().getParent());
+ projectPaths.add(getParentPath(path));
break;
case TEST_CLASSES:
- projectPaths.add(path.getParent().getParent());
+ projectPaths.add(getParentPath(path));
break;
case JAR:
isJar = true;
classesPaths.add(path);
- projectPaths.add(path.getParent().getParent());
+ projectPaths.add(getParentPath(path));
break;
case MUTABLE_JAR:
// project, class, and target are all the same.
// also, we don't need any prefix (see constructor), hence
passing GRADLE as the build tool
- return new QuarkusAppPaths(Collections.singleton(path),
Collections.singleton(path), false, BuildTool.GRADLE, path);
+ return new
QuarkusAppPaths(Collections.singletonList(path), Collections.singleton(path),
false);
case UNKNOWN:
classesPaths.add(path);
projectPaths.add(path);
break;
}
}
- return new QuarkusAppPaths(projectPaths, classesPaths, isJar, bt,
outputTarget);
+ return new QuarkusAppPaths(new ArrayList<>(projectPaths),
classesPaths, isJar);
+ }
+
+ private static Path getParentPath(Path path) {
+ if (AppPaths.BT.equals(BuildTool.GRADLE)) {
+ return path.getParent().getParent().getParent().getParent();
+ } else {
+ return path.getParent().getParent();
+ }
}
private static PathType getPathType(Path archiveLocation) {
if (archiveLocation.endsWith(MUTABLE_JAR_PATH)) {
return PathType.MUTABLE_JAR;
}
- if (archiveLocation.endsWith(CLASSES_PATH)) {
+ if (archiveLocation.endsWith(AppPaths.BT.CLASSES_PATH)) {
return PathType.CLASSES;
}
- if (archiveLocation.endsWith(TEST_CLASSES_PATH)) {
+ if (archiveLocation.endsWith(AppPaths.BT.TEST_CLASSES_PATH)) {
return PathType.TEST_CLASSES;
}
// Quarkus generates a file with extension .jar.original when doing a
native compilation of a uberjar
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]