forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkusio#1683 from geoand/appconfig
Introduce ApplicationConfig
- Loading branch information
Showing
7 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
core/deployment/src/main/java/io/quarkus/deployment/ApplicationConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.quarkus.deployment; | ||
|
||
import io.quarkus.runtime.annotations.ConfigItem; | ||
import io.quarkus.runtime.annotations.ConfigPhase; | ||
import io.quarkus.runtime.annotations.ConfigRoot; | ||
|
||
@ConfigRoot(phase = ConfigPhase.BUILD_TIME) | ||
public class ApplicationConfig { | ||
|
||
/** | ||
* The name of the application. | ||
* If not set, defaults to the name of the project. | ||
*/ | ||
@ConfigItem | ||
public String name; | ||
|
||
/** | ||
* The version of the application. | ||
* If not set, defaults to the version of the project | ||
*/ | ||
@ConfigItem | ||
public String version; | ||
} |
32 changes: 32 additions & 0 deletions
32
core/deployment/src/main/java/io/quarkus/deployment/ApplicationInfoUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.quarkus.deployment; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Properties; | ||
|
||
import io.quarkus.bootstrap.model.AppArtifact; | ||
|
||
public final class ApplicationInfoUtil { | ||
|
||
public static final String APPLICATION_INFO_PROPERTIES = "application-info.properties"; | ||
public static final String META_INF = "META-INF"; | ||
|
||
private ApplicationInfoUtil() { | ||
} | ||
|
||
// these properties are used as default values for ApplicationInfoBuildItem | ||
public static void writeApplicationInfoProperties(AppArtifact appArtifact, Path appClassesDir) { | ||
Properties properties = new Properties(); | ||
properties.setProperty("artifactId", appArtifact.getArtifactId()); | ||
properties.setProperty("version", appArtifact.getVersion()); | ||
try { | ||
appClassesDir.resolve(META_INF).toFile().mkdir(); | ||
File file = appClassesDir.resolve(META_INF).resolve(APPLICATION_INFO_PROPERTIES).toFile(); | ||
properties.store(new FileOutputStream(file), "Generated file; do not edit manually"); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
core/deployment/src/main/java/io/quarkus/deployment/builditem/ApplicationInfoBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.quarkus.deployment.builditem; | ||
|
||
import org.jboss.builder.item.SimpleBuildItem; | ||
|
||
public final class ApplicationInfoBuildItem extends SimpleBuildItem { | ||
|
||
private final String name; | ||
private final String version; | ||
|
||
public ApplicationInfoBuildItem(String name, String version) { | ||
this.name = name; | ||
this.version = version; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getVersion() { | ||
return version; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
core/deployment/src/main/java/io/quarkus/deployment/steps/ApplicationInfoBuildStep.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package io.quarkus.deployment.steps; | ||
|
||
import static io.quarkus.deployment.ApplicationInfoUtil.APPLICATION_INFO_PROPERTIES; | ||
import static io.quarkus.deployment.ApplicationInfoUtil.META_INF; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Enumeration; | ||
import java.util.Properties; | ||
|
||
import io.quarkus.deployment.ApplicationConfig; | ||
import io.quarkus.deployment.annotations.BuildStep; | ||
import io.quarkus.deployment.builditem.ApplicationInfoBuildItem; | ||
|
||
public class ApplicationInfoBuildStep { | ||
|
||
private static final String PROPERTIES_FILE_TO_READ = META_INF + File.separator + APPLICATION_INFO_PROPERTIES; | ||
|
||
private static final String ARTIFACT_ID_KEY = "artifactId"; | ||
private static final String VERSION_KEY = "version"; | ||
private static final String UNSET_VALUE = "unset"; | ||
|
||
@BuildStep | ||
public ApplicationInfoBuildItem create(ApplicationConfig applicationConfig) { | ||
final String userConfiguredName = applicationConfig.name; | ||
final String userConfiguredVersion = applicationConfig.version; | ||
|
||
final Properties applicationInfoProperties = getApplicationInfoProperties(); | ||
|
||
return new ApplicationInfoBuildItem( | ||
useIfNotEmpty(userConfiguredName, applicationInfoProperties.getProperty(ARTIFACT_ID_KEY, UNSET_VALUE)), | ||
useIfNotEmpty(userConfiguredVersion, applicationInfoProperties.getProperty(VERSION_KEY, UNSET_VALUE))); | ||
} | ||
|
||
private Properties getApplicationInfoProperties() { | ||
ClassLoader cl = Thread.currentThread().getContextClassLoader(); | ||
if (cl == null) { | ||
cl = ApplicationInfoBuildStep.class.getClassLoader(); | ||
} | ||
try { | ||
final Properties p = new Properties(); | ||
// work around #1477 | ||
final Enumeration<URL> resources = cl == null ? ClassLoader.getSystemResources(PROPERTIES_FILE_TO_READ) | ||
: cl.getResources(PROPERTIES_FILE_TO_READ); | ||
if (resources.hasMoreElements()) { | ||
final URL url = resources.nextElement(); | ||
try (InputStream is = url.openStream()) { | ||
try (InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8)) { | ||
p.load(isr); | ||
} | ||
} | ||
} | ||
return p; | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Cannot read application-info.properties", e); | ||
} | ||
} | ||
|
||
private String useIfNotEmpty(String value, String defaultValue) { | ||
return (value != null) && !value.isEmpty() ? value : defaultValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters