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

Add required properties for expansion in the build ConfigSource #14809

Merged
merged 1 commit into from Apr 5, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.util.List;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand Down Expand Up @@ -47,6 +48,9 @@ public abstract class QuarkusBootstrapMojo extends AbstractMojo {
@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;

@Parameter(defaultValue = "${session}", readonly = true)
private MavenSession session;

@Parameter(defaultValue = "${project.build.directory}")
private File buildDir;

Expand Down Expand Up @@ -125,6 +129,10 @@ protected MavenProject mavenProject() {
return project;
}

public MavenSession mavenSession() {
return session;
}

protected File buildDir() {
return buildDir;
}
Expand Down
@@ -1,5 +1,8 @@
package io.quarkus.maven;

import static io.smallrye.common.expression.Expression.Flag.LENIENT_SYNTAX;
import static io.smallrye.common.expression.Expression.Flag.NO_TRIM;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
Expand All @@ -25,6 +28,7 @@
import io.quarkus.bootstrap.model.PathsCollection;
import io.quarkus.bootstrap.resolver.maven.BootstrapMavenException;
import io.quarkus.bootstrap.resolver.maven.MavenArtifactResolver;
import io.smallrye.common.expression.Expression;

@Component(role = QuarkusBootstrapProvider.class, instantiationStrategy = "singleton")
public class QuarkusBootstrapProvider implements Closeable {
Expand Down Expand Up @@ -163,6 +167,22 @@ protected QuarkusBootstrap bootstrapQuarkus(QuarkusBootstrapMojo mojo) throws Mo
effectiveProperties.putIfAbsent("quarkus.application.name", mojo.mavenProject().getArtifactId());
effectiveProperties.putIfAbsent("quarkus.application.version", mojo.mavenProject().getVersion());

// Add other properties that may be required for expansion
for (Object value : effectiveProperties.values()) {
for (String reference : Expression.compile((String) value, LENIENT_SYNTAX, NO_TRIM).getReferencedStrings()) {
String referenceValue = mojo.mavenSession().getUserProperties().getProperty(reference);
if (referenceValue != null) {
effectiveProperties.setProperty(reference, referenceValue);
continue;
}

referenceValue = projectProperties.getProperty(reference);
if (referenceValue != null) {
effectiveProperties.setProperty(reference, referenceValue);
}
}
}

QuarkusBootstrap.Builder builder = QuarkusBootstrap.builder()
.setAppArtifact(projectArtifact(mojo))
.setMavenArtifactResolver(artifactResolver(mojo))
Expand Down
Expand Up @@ -1028,4 +1028,12 @@ public void testThatGenerateCodeGoalIsNotTriggeredIfNotConfigured() throws IOExc
assertThat(running.log()).contains("Copying 1 resource"); // maven-resource-plugin
assertThat(running.log()).contains("Compiling 2 source files"); // maven-compiler-plugin
}

@Test
public void testPropertyExpansion() throws IOException, MavenInvocationException {
testDir = initProject("projects/property-expansion");
runAndCheck();
assertThat(DevModeTestUtils.getHttpResponse("/app/hello/")).isEqualTo("hello");
assertThat(DevModeTestUtils.getHttpResponse("/app/hello/applicationName")).isEqualTo("myapp");
}
}
@@ -0,0 +1,103 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.acme</groupId>
<artifactId>acme</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
<quarkus.platform.version>@project.version@</quarkus.platform.version>
<quarkus-plugin.version>@project.version@</quarkus-plugin.version>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>

<expansion.application.name>myapp</expansion.application.name>
<quarkus.application.name>${expansion.application.name}</quarkus.application.name>

<expansion.image.tag>tag</expansion.image.tag>
<quarkus.container-image.tag>${expansion.image.tag}</quarkus.container-image.tag>
radcortez marked this conversation as resolved.
Show resolved Hide resolved
</properties>
<dependencyManagement>
<dependencies>
<!-- insert managed dependencies here -->
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- insert test dependencies here -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-container-image-docker</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>regex-property</id>
<phase>validate</phase>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>expansion.image.tag</name>
<value>tag</value>
<regex>tag</regex>
<replacement>tag</replacement>
<failIfNoMatch>false</failIfNoMatch>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>
</profile>
</profiles>
</project>
@@ -0,0 +1,27 @@
package org.acme;

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

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;

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

@ConfigProperty(name = "quarkus.application.name")
String applicationName;

@GET
@Path("/applicationName")
public String applicationName() {
return applicationName;
}
}
@@ -0,0 +1,9 @@
package org.acme;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/app")
public class MyApplication extends Application {

}
@@ -0,0 +1,156 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>acme - 1.0-SNAPSHOT</title>
<style>
h1, h2, h3, h4, h5, h6 {
margin-bottom: 0.5rem;
font-weight: 400;
line-height: 1.5;
}

h1 {
font-size: 2.5rem;
}

h2 {
font-size: 2rem
}

h3 {
font-size: 1.75rem
}

h4 {
font-size: 1.5rem
}

h5 {
font-size: 1.25rem
}

h6 {
font-size: 1rem
}

.lead {
font-weight: 300;
font-size: 2rem;
}

.banner {
font-size: 2.7rem;
margin: 0;
padding: 2rem 1rem;
background-color: #00A1E2;
color: white;
}

body {
margin: 0;
font-family: -apple-system, system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
}

code {
font-family: SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
font-size: 87.5%;
color: #e83e8c;
word-break: break-word;
}

.left-column {
padding: .75rem;
max-width: 75%;
min-width: 55%;
}

.right-column {
padding: .75rem;
max-width: 25%;
}

.container {
display: flex;
width: 100%;
}

li {
margin: 0.75rem;
}

.right-section {
margin-left: 1rem;
padding-left: 0.5rem;
}

.right-section h3 {
padding-top: 0;
font-weight: 200;
}

.right-section ul {
border-left: 0.3rem solid #00A1E2;
list-style-type: none;
padding-left: 0;
}

</style>
</head>
<body>

<div class="banner lead">
Your new Cloud-Native application is ready!
</div>

<div class="container">
<div class="left-column">
<p class="lead"> Congratulations, you have created a new Quarkus application.</p>

<h2>Why do you see this?</h2>

<p>This page is served by Quarkus. The source is in
<code>src/main/resources/META-INF/resources/index.html</code>.</p>

<h2>What can I do from here?</h2>

<p>If not already done, run the application in <em>dev mode</em> using: <code>mvn compile quarkus:dev</code>.
</p>
<ul>
<li>Add REST resources, Servlets, functions and other services in <code>src/main/java</code>.</li>
<li>Your static assets are located in <code>src/main/resources/META-INF/resources</code>.</li>
<li>Configure your application in <code>src/main/resources/application.properties</code>.
</li>
</ul>

<h2>Do you like Quarkus?</h2>
<p>Go give it a star on <a href="https://github.com/quarkusio/quarkus">GitHub</a>.</p>

<h2>How do I get rid of this page?</h2>
<p>Just delete the <code>src/main/resources/META-INF/resources/index.html</code> file.</p>
</div>
<div class="right-column">
<div class="right-section">
<h3>Application</h3>
<ul>
<li>GroupId: org.acme</li>
<li>ArtifactId: acme</li>
<li>Version: 1.0-SNAPSHOT</li>
<li>Quarkus Version: 999-SNAPSHOT</li>
</ul>
</div>
<div class="right-section">
<h3>Next steps</h3>
<ul>
<!-- the url have been erased on purpose -->
<li><a href="#">Setup your IDE</a></li>
<li><a href="#">Getting started</a></li>
<li><a href="#">Documentation</a></li>
</ul>
</div>
</div>
</div>


</body>
</html>