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

Build Properties #331

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions alpha-cli-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ tasks.create<Jar>("bundledJar") {
archiveFileName.set("${project.name}-${project.version}-bundled.jar")

exclude("META-INF/DEPENDENCIES")

filesMatching("META-INF/build.properties") {
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}

/*
* In order to make sure we don"t overwrite NOTICE and LICENSE files coming from dependency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.function.Consumer;

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

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

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

CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_NUM_ANSWER_SETS);
CommandLineParser.CLI_OPTS.addOption(CommandLineParser.OPT_FILTER);
Expand Down Expand Up @@ -223,8 +226,10 @@ private void initializeGlobalOptionHandlers() {
/*
* below put invocations are used to "register" the handler methods for each commandline option
*/
// help is handled separately, therefore dummy handler
// help and version are handled separately, therefore dummy handlers
this.globalOptionHandlers.put(CommandLineParser.OPT_HELP.getOpt(), (o, c) -> { });
this.globalOptionHandlers.put(CommandLineParser.OPT_VERSION.getOpt(), (o, c) -> { });

this.globalOptionHandlers.put(CommandLineParser.OPT_GROUNDER.getOpt(), this::handleGrounder);
this.globalOptionHandlers.put(CommandLineParser.OPT_SOLVER.getOpt(), this::handleSolver);
this.globalOptionHandlers.put(CommandLineParser.OPT_NOGOOD_STORE.getOpt(), this::handleNogoodStore);
Expand Down Expand Up @@ -269,6 +274,9 @@ public AlphaConfig parseCommandLine(String[] args) throws ParseException {
if (commandLine.hasOption(CommandLineParser.OPT_HELP.getOpt())) {
LOGGER.debug("Found help option!");
this.handleHelp();
} else if (commandLine.hasOption(CommandLineParser.OPT_VERSION.getLongOpt())) {
LOGGER.debug("Found version option!");
this.handleVersion();
} else {
this.validate(commandLine);
}
Expand Down Expand Up @@ -308,6 +316,16 @@ public String getUsageMessage() {
return helpBuffer.toString();
}

public String getVersion() {
try {
Properties properties = new Properties();
properties.load(CommandLineParser.class.getResourceAsStream("/META-INF/build.properties"));
return properties.getProperty("project.version");
} catch (Exception e) {
return "unknown (" + e.getMessage() + ")";
}
}

private void validate(CommandLine commandLine) throws ParseException {
if (!commandLine.hasOption(CommandLineParser.OPT_INPUT.getOpt()) && !commandLine.hasOption(CommandLineParser.OPT_ASPSTRING.getOpt())) {
throw new ParseException("Missing input source - need to specifiy either a file (" + CommandLineParser.OPT_INPUT.getOpt() + ") or a string ("
Expand All @@ -330,6 +348,10 @@ private void handleHelp() {
this.abortAction.accept(this.getUsageMessage());
}

private void handleVersion() {
this.abortAction.accept(this.getVersion());
}

private void handleInput(Option opt, InputConfig cfg) {
String optVal = opt.getValue().trim();
cfg.getFiles().add(optVal);
Expand Down
10 changes: 10 additions & 0 deletions buildSrc/src/main/kotlin/alpha.java-common-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ tasks.jacocoTestReport {
}
}

tasks.register<WriteProperties>("buildProperties") {
outputFile = file("${sourceSets["main"].output.resourcesDir}/META-INF/build.properties")
encoding = "UTF-8"
property("project.version", project.version)
}

tasks.processResources {
dependsOn("buildProperties")
}

publishing {
publications {
create<MavenPublication>("binary") {
Expand Down