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

Import properties from build.gradle and remove hardcoded quarkus.package.output-directory #29971

Merged
merged 1 commit into from
Jan 9, 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 @@ -3,13 +3,16 @@
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.StringJoiner;
import java.util.stream.Collectors;

import javax.annotation.Nullable;

import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;
Expand All @@ -34,6 +37,8 @@ public class QuarkusPluginExtension {
private final Project project;

private final Property<String> finalName;

private Map<String, String> quarkusBuildProperties;
private final SourceSetExtension sourceSetExtension;

public QuarkusPluginExtension(Project project) {
Expand All @@ -43,6 +48,7 @@ public QuarkusPluginExtension(Project project) {
finalName.convention(project.provider(() -> String.format("%s-%s", project.getName(), project.getVersion())));

this.sourceSetExtension = new SourceSetExtension();
quarkusBuildProperties = new HashMap<>();
}

public void beforeTest(Test task) {
Expand Down Expand Up @@ -182,4 +188,13 @@ public Path appJarOrClasses() {
}
return classesDir;
}

public Map<String, String> getQuarkusBuildProperties() {
return quarkusBuildProperties;
}

public void set(String name, @Nullable String value) {
quarkusBuildProperties.put(String.format("quarkus.%s", name), value);
}
Comment on lines +192 to +198
Copy link
Contributor

Choose a reason for hiding this comment

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

This should use lazy configuration APIs. So quarkusBuildProperties should not be of type Map<String, String> but MapProperty<String, String>. At the same time there should be an overload for set where value can be passed as a Property<String>.

Another issue with this solution is that is assumes everything is a string, but that's not the case. For example the package.output-directoy should probably be of type DirectoryProperty (see https://docs.gradle.org/current/userguide/lazy_configuration.html#working_with_files_in_lazy_properties).

Copy link
Member

Choose a reason for hiding this comment

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

@britter thanks a lot for the review. Let us know if you would like to submit a PR yourself. That'd be much appreciated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the feedback @britter , I'm reviewing the docs. I can also submit a PR to fix this, if you won't.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see myself doing this before the end of the month. So if you have some time @jacobdotcosta please go ahead.

Copy link
Member

Choose a reason for hiding this comment

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

It doesn't look like it was fixed yet. @britter in case you have some time to contribute the fixes you mentioned previously, it'll be much appreciated. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

@aloubyansky I managed to address the first part of it in #30713. I'm not sure it's possible to address the second part using a map based approached.


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.quarkus.gradle.tasks;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -16,6 +18,7 @@
import org.gradle.api.file.FileCollection;
import org.gradle.api.java.archives.Attributes;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
Expand All @@ -35,15 +38,20 @@
import io.quarkus.maven.dependency.GACTV;
import io.quarkus.runtime.util.StringUtil;

@CacheableTask
public abstract class QuarkusBuild extends QuarkusTask {

private static final String NATIVE_PROPERTY_NAMESPACE = "quarkus.native";
private static final String MANIFEST_SECTIONS_PROPERTY_PREFIX = "quarkus.package.manifest.manifest-sections";
private static final String MANIFEST_ATTRIBUTES_PROPERTY_PREFIX = "quarkus.package.manifest.attributes";

private static final String OUTPUT_DIRECTORY = "quarkus.package.output-directory";

private List<String> ignoredEntries = new ArrayList<>();
private Manifest manifest = new Manifest();

private Properties applicationProperties = new Properties();

@Inject
public QuarkusBuild() {
super("Quarkus builds a runner jar based on the build jar");
Expand Down Expand Up @@ -131,7 +139,8 @@ public File getNativeRunner() {

@OutputDirectory
public File getFastJar() {
return new File(getProject().getBuildDir(), "quarkus-app");
return new File(getProject().getBuildDir(),
this.getPropValueWithPrecedence(OUTPUT_DIRECTORY, java.util.Optional.of("quarkus-app")));
}

@TaskAction
Expand Down Expand Up @@ -223,4 +232,37 @@ private String expandConfigurationKey(String shortKey) {
}
return String.format("%s.%s", NATIVE_PROPERTY_NAMESPACE, hyphenatedKey);
}

private String getPropValueWithPrecedence(final String propName, final java.util.Optional<String> defaultValue) {
if (applicationProperties.isEmpty()) {
FileCollection classpathFiles = getClasspath()
.filter(file -> "application.properties".equalsIgnoreCase(file.getName()));
classpathFiles.forEach(file -> {
FileInputStream appPropsIS = null;
try {
appPropsIS = new FileInputStream(file.getAbsoluteFile());
applicationProperties.load(appPropsIS);
appPropsIS.close();
} catch (IOException e) {
if (appPropsIS != null) {
try {
appPropsIS.close();
} catch (IOException ex) {
// Ignore exception closing.
}
}
}
});
}
if (extension().getQuarkusBuildProperties().containsKey(propName)) {
return extension().getQuarkusBuildProperties().get(propName);
} else if (applicationProperties.contains(propName)) {
return applicationProperties.getProperty(propName);
} else if (getQuarkusBuildEnvProperties().containsKey(propName)) {
return getQuarkusBuildEnvProperties().get(propName);
} else if (defaultValue.isPresent()) {
return defaultValue.get();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ protected Properties getBuildSystemProperties(ResolvedDependency appArtifact) {
realProperties.setProperty(key, (String) value);
}
}
if (!extension().getQuarkusBuildProperties().isEmpty()) {
extension().getQuarkusBuildProperties().entrySet().stream().filter(entry -> entry.getKey().startsWith("quarkus."))
.forEach(entry -> {
realProperties.put(entry.getKey(), entry.getValue());
});
}
realProperties.putIfAbsent("quarkus.application.name", appArtifact.getArtifactId());
realProperties.putIfAbsent("quarkus.application.version", appArtifact.getVersion());
return realProperties;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
plugins {
id 'java'
id 'io.quarkus' apply false
}

repositories {
mavenLocal {
content {
includeGroupByRegex 'io.quarkus.*'
}
}
mavenCentral()
}

test {
systemProperty "java.util.logging.manager", "org.jboss.logmanager.LogManager"
}

group = 'io.quarkus.gradle.test'
version = '1.0'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
quarkusPlatformArtifactId=quarkus-bom
quarkusPlatformGroupId=io.quarkus
quarkusPluginId=io.quarkus
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pluginManagement {
repositories {
mavenLocal {
content {
includeGroupByRegex 'io.quarkus.*'
}
}
mavenCentral()
gradlePluginPortal()
}
plugins {
id "${quarkusPluginId}" version "${quarkusPluginVersion}"
}
}

include 'with-application-properties'
include 'with-build-configuration'
include 'without-configuration'
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
plugins {
id 'java'
id 'io.quarkus'
}

repositories {
mavenLocal {
content {
includeGroupByRegex 'io.quarkus.*'
}
}
mavenCentral()
}

dependencies {
implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
implementation 'io.quarkus:quarkus-resteasy'

testImplementation 'io.quarkus:quarkus-junit5'
testImplementation 'io.rest-assured:rest-assured'
}

group 'org.acme'
version '1.0.0-SNAPSHOT'

quarkus {
properties {
set("package.type", "fast-jar")
set("package.output-directory", "build-gradle-output-dir")
}
}

compileJava {
options.encoding = 'UTF-8'
options.compilerArgs << '-parameters'
}

compileTestJava {
options.encoding = 'UTF-8'
}

test {
systemProperty "java.util.logging.manager", "org.jboss.logmanager.LogManager"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pluginManagement {
repositories {
mavenLocal {
content {
includeGroupByRegex 'io.quarkus.*'
}
}
mavenCentral()
gradlePluginPortal()
}
plugins {
id "${quarkusPluginId}" version "${quarkusPluginVersion}"
}
}
rootProject.name='with-application-properties'
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.acme;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.config.inject.ConfigProperty;

@Path("/hello")
public class ExampleResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "hello";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Configuration file
# key = value
my-app-name=${quarkus.application.name}
quarkus.package.type=uber-jar
quarkus.package.output-directory=application-properties-output-dir
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Configuration file
# key = value
example.message=Hello from Test
test-only=Test only
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
plugins {
id 'java'
id 'io.quarkus'
}

repositories {
mavenLocal {
content {
includeGroupByRegex 'io.quarkus.*'
}
}
mavenCentral()
}

dependencies {
implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
implementation 'io.quarkus:quarkus-resteasy'

testImplementation 'io.quarkus:quarkus-junit5'
testImplementation 'io.rest-assured:rest-assured'
}

group 'org.acme'
version '1.0.0-SNAPSHOT'

quarkus {
properties {
set("package.type", "uber-jar")
set("package.output-directory", "build-gradle-output-dir")
}
}

compileJava {
options.encoding = 'UTF-8'
options.compilerArgs << '-parameters'
}

compileTestJava {
options.encoding = 'UTF-8'
}

test {
systemProperty "java.util.logging.manager", "org.jboss.logmanager.LogManager"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pluginManagement {
repositories {
mavenLocal {
content {
includeGroupByRegex 'io.quarkus.*'
}
}
mavenCentral()
gradlePluginPortal()
}
plugins {
id "${quarkusPluginId}" version "${quarkusPluginVersion}"
}
}
rootProject.name='with-build-configuration'
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.acme;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.config.inject.ConfigProperty;

@Path("/hello")
public class ExampleResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "hello";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Configuration file
# key = value
my-app-name=${quarkus.application.name}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Configuration file
# key = value
example.message=Hello from Test
test-only=Test only
Loading