Skip to content

Commit 1293c40

Browse files
committed
gradle: Add Build Properties
1 parent d42818a commit 1293c40

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

alpha-cli-app/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ tasks.create<Jar>("bundledJar") {
4040
archiveFileName.set("${project.name}-${project.version}-bundled.jar")
4141

4242
exclude("META-INF/DEPENDENCIES")
43+
44+
filesMatching("META-INF/build.properties") {
45+
duplicatesStrategy = DuplicatesStrategy.INCLUDE
46+
}
4347

4448
/*
4549
* In order to make sure we don"t overwrite NOTICE and LICENSE files coming from dependency

alpha-cli-app/src/main/java/at/ac/tuwien/kr/alpha/app/config/CommandLineParser.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.io.PrintWriter;
3333
import java.util.HashMap;
3434
import java.util.Map;
35+
import java.util.Properties;
3536
import java.util.function.Consumer;
3637

3738
import org.apache.commons.cli.CommandLine;
@@ -72,6 +73,7 @@ public class CommandLineParser {
7273

7374
// "special", i.e. non-configuration options
7475
private static final Option OPT_HELP = Option.builder("h").longOpt("help").hasArg(false).desc("shows this help").build();
76+
private static final Option OPT_VERSION = Option.builder().longOpt("version").hasArg(false).desc("prints the version and exits").build();
7577

7678
// input-specific options
7779
private static final Option OPT_INPUT = Option.builder("i").longOpt("input").hasArg(true).argName("file").type(FileInputStream.class)
@@ -158,6 +160,7 @@ public class CommandLineParser {
158160
* Below code adds all options defined above to CLI_OPTS - needed for parsing
159161
*/
160162
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_HELP);
163+
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_VERSION);
161164

162165
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_NUM_ANSWER_SETS);
163166
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_FILTER);
@@ -223,8 +226,10 @@ private void initializeGlobalOptionHandlers() {
223226
/*
224227
* below put invocations are used to "register" the handler methods for each commandline option
225228
*/
226-
// help is handled separately, therefore dummy handler
229+
// help and version are handled separately, therefore dummy handlers
227230
this.globalOptionHandlers.put(CommandLineParser.OPT_HELP.getOpt(), (o, c) -> { });
231+
this.globalOptionHandlers.put(CommandLineParser.OPT_VERSION.getOpt(), (o, c) -> { });
232+
228233
this.globalOptionHandlers.put(CommandLineParser.OPT_GROUNDER.getOpt(), this::handleGrounder);
229234
this.globalOptionHandlers.put(CommandLineParser.OPT_SOLVER.getOpt(), this::handleSolver);
230235
this.globalOptionHandlers.put(CommandLineParser.OPT_NOGOOD_STORE.getOpt(), this::handleNogoodStore);
@@ -269,6 +274,9 @@ public AlphaConfig parseCommandLine(String[] args) throws ParseException {
269274
if (commandLine.hasOption(CommandLineParser.OPT_HELP.getOpt())) {
270275
LOGGER.debug("Found help option!");
271276
this.handleHelp();
277+
} else if (commandLine.hasOption(CommandLineParser.OPT_VERSION.getLongOpt())) {
278+
LOGGER.debug("Found version option!");
279+
this.handleVersion();
272280
} else {
273281
this.validate(commandLine);
274282
}
@@ -308,6 +316,16 @@ public String getUsageMessage() {
308316
return helpBuffer.toString();
309317
}
310318

319+
public String getVersion() {
320+
try {
321+
Properties properties = new Properties();
322+
properties.load(CommandLineParser.class.getResourceAsStream("/META-INF/build.properties"));
323+
return properties.getProperty("project.version");
324+
} catch (Exception e) {
325+
return "unknown (" + e.getMessage() + ")";
326+
}
327+
}
328+
311329
private void validate(CommandLine commandLine) throws ParseException {
312330
if (!commandLine.hasOption(CommandLineParser.OPT_INPUT.getOpt()) && !commandLine.hasOption(CommandLineParser.OPT_ASPSTRING.getOpt())) {
313331
throw new ParseException("Missing input source - need to specifiy either a file (" + CommandLineParser.OPT_INPUT.getOpt() + ") or a string ("
@@ -330,6 +348,10 @@ private void handleHelp() {
330348
this.abortAction.accept(this.getUsageMessage());
331349
}
332350

351+
private void handleVersion() {
352+
this.abortAction.accept(this.getVersion());
353+
}
354+
333355
private void handleInput(Option opt, InputConfig cfg) {
334356
String optVal = opt.getValue().trim();
335357
cfg.getFiles().add(optVal);

buildSrc/src/main/kotlin/alpha.java-common-conventions.gradle.kts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ tasks.jacocoTestReport {
6464
}
6565
}
6666

67+
tasks.register<WriteProperties>("buildProperties") {
68+
outputFile = file("${sourceSets["main"].output.resourcesDir}/META-INF/build.properties")
69+
encoding = "UTF-8"
70+
property("project.version", project.version)
71+
}
72+
73+
tasks.processResources {
74+
dependsOn("buildProperties")
75+
}
76+
6777
publishing {
6878
publications {
6979
create<MavenPublication>("binary") {

0 commit comments

Comments
 (0)