Skip to content

Commit

Permalink
import properties from build.gradle and remove hardcoded quarkus.pack…
Browse files Browse the repository at this point in the history
…age.output-directory value

wip

tests: IT for the build.gradle configuration

code cleanup

feat: import from build properties

test: defaults test for projects without configuration

chore: removed uncecessary hardcoded quarkus version
  • Loading branch information
jacobdotcosta committed Dec 20, 2022
1 parent 0a79d6a commit ac97157
Show file tree
Hide file tree
Showing 22 changed files with 461 additions and 1 deletion.
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);
}

}
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,27 @@ 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 -> {
try {
applicationProperties.load(new FileInputStream(file.getAbsoluteFile()));
} catch (IOException e) {
}
});
}
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,7 @@
# Configuration file
# key = value
example.message=Hello from Test
test-only=Test only
#quarkus.package.type=fast-jar


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,6 @@
# Configuration file
# key = value
example.message=Hello from Test
test-only=Test only


Loading

0 comments on commit ac97157

Please sign in to comment.