Skip to content
Snippets Groups Projects
Commit b9c5a6e4 authored by Robert Goldmann's avatar Robert Goldmann
Browse files

#663 - pass arguments via command line

parent 8bbd62e0
Branches
Tags
No related merge requests found
......@@ -13,6 +13,7 @@
<properties>
<h2database.version>1.4.199</h2database.version>
<commons-cli.version>1.5.0</commons-cli.version>
</properties>
<dependencies>
......@@ -45,6 +46,12 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>${commons-cli.version}</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package de.deadlocker8.budgetmaster.databasemigrator;
public enum CommandLineOptions
{
SOURCE_URL("spring.datasource.jdbc-url", "source h2 database JDBC url, e.g. jdbc:h2:/C:/Users/Admin/AppData/Roaming/Deadlocker/BudgetMaster/debug/budgetmaster"),
DESTINATION_URL("spring.seconddatasource.jdbc-url", "destination postresql database JDBC url, e.g. jdbc:postgresql://localhost:5432/budgetmaster"),
DESTINATION_USER_NAME("spring.seconddatasource.username", "destination postresql user name, e.g. budgetmaster"),
DESTINATION_PASSWORD("spring.seconddatasource.password", "destination postresql password, e.g. BudgetMaster");
private final String name;
private final String description;
CommandLineOptions(String name, String description)
{
this.name = name;
this.description = description;
}
public String getName()
{
return name;
}
public String getDescription()
{
return description;
}
}
package de.deadlocker8.budgetmaster.databasemigrator;
import org.apache.commons.cli.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
......@@ -11,6 +14,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DatabaseMigratorMain implements CommandLineRunner
{
private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseMigratorMain.class);
private final JobLauncher jobLauncher;
@Qualifier("migrateJob")
......@@ -22,11 +27,44 @@ public class DatabaseMigratorMain implements CommandLineRunner
this.migrateJob = migrateJob;
}
public static void main(String[] args)
public static void main(String[] args) throws ParseException
{
final Options commandLineOptions = new Options();
for(CommandLineOptions option : CommandLineOptions.values())
{
commandLineOptions.addRequiredOption(null, option.getName(), true, option.getDescription());
}
showHelpIfRequested(args, commandLineOptions);
final CommandLineParser parser = new DefaultParser();
final CommandLine cmd = parser.parse(commandLineOptions, args);
for(CommandLineOptions option : CommandLineOptions.values())
{
LOGGER.debug("{}={}", option.getName(), cmd.getOptionValue(option.getName()));
}
SpringApplication.run(DatabaseMigratorMain.class, args);
}
private static void showHelpIfRequested(String[] args, Options availableCommandLineOptions) throws ParseException
{
final Options commandLineOptions = new Options();
commandLineOptions.addOption("h", "help", false, "Print this help");
final CommandLineParser parser = new DefaultParser();
final CommandLine cmd = parser.parse(commandLineOptions, args, true);
if(cmd.hasOption("help"))
{
final HelpFormatter formatter = new HelpFormatter();
formatter.setWidth(200);
formatter.printHelp("BudgetMasterMigrator", availableCommandLineOptions);
System.exit(0);
}
}
@Override
public void run(String... args) throws Exception
{
......
spring.datasource.jdbc-url=jdbc:h2:/C:/Users/RobertG/AppData/Roaming/Deadlocker/BudgetMaster/debug/budgetmaster
# commented lines should be passed by command line and are only retained as example
#spring.datasource.jdbc-url=
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.seconddatasource.jdbc-url=jdbc:postgresql://localhost:5432/budgetmaster
spring.seconddatasource.username=budgetmaster
spring.seconddatasource.password=BudgetMaster
#spring.seconddatasource.jdbc-url=
#spring.seconddatasource.username=
#spring.seconddatasource.password=
spring.seconddatasource.driver-class-name=org.postgresql.Driver
spring.jpa.database=default
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment