Skip to content

Commit

Permalink
[incubator-kie-issues#1068] Fix gradle build output (#5839)
Browse files Browse the repository at this point in the history
* [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]>
  • Loading branch information
gitgabrio and Gabriele-Cardosi authored Apr 17, 2024
1 parent e368e23 commit faf8ac4
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 151 deletions.
9 changes: 9 additions & 0 deletions drools-model/drools-codegen-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,63 @@
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";

Expand All @@ -66,8 +101,8 @@ public static AppPaths.BuildTool findBuildTool() {

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);
}

/**
Expand All @@ -77,7 +112,27 @@ public static AppPaths fromProjectDir(Path projectDir, Path outputTarget) {
* @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);
}

/**
Expand All @@ -87,14 +142,14 @@ public static AppPaths fromTestDir(Path projectDir) {
* @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);
Expand Down Expand Up @@ -137,12 +192,6 @@ public String toString() {
'}';
}

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");
Expand All @@ -151,20 +200,14 @@ static Path[] getJarPaths(boolean isInnerJar, Collection<Path> innerClassesPaths
}
}

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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ enum Category {
/**
* 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,70 @@
* 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;
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;
}
Expand All @@ -58,32 +92,23 @@ public Builder(String classesDir, String sourcesDir, String resourcesDir, String
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;
}
Expand All @@ -102,11 +127,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 All @@ -120,10 +141,6 @@ public Path getClassesDir() {
return classesDir;
}

public Path getSourcesDir() {
return sourcesDir;
}

public Path getResourcePath() {
return resourcePath;
}
Expand All @@ -132,7 +149,7 @@ public Path getScaffoldedSourcesDir() {
return scaffoldedSourcesDir;
}

private void writeGeneratedFile(GeneratedFile f, Path location) throws IOException {
void writeGeneratedFile(GeneratedFile f, Path location) throws IOException {
if (location == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ protected abstract static class AbstractBuilder implements Builder {
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() {
}
Expand Down
Loading

0 comments on commit faf8ac4

Please sign in to comment.