Running Spring Batch Jobs from the Command Line

I don’t know how to call a task defined in Spring Batch using CommandLineJobRunner , the documentation is not enough for me.

I followed the Spring Batch official guide for writing jobs in a Spring Package using Java annotations, for example. @EnableBatchProcessing because I wanted to avoid the XML configuration files for job descriptions, steps, etc.

So far, I:

  • configuration class ( com.package.bla.bla.ClassContainingTheBatchConfiguration see below), where I placed all the elements defining ItemReader , ItemProcessor , ItemWriter , Job and Step (with return jobs.get("nameOfTheJob") see below) using antagonism @Bean .
  • a class with the main method with SpringApplication.run(...) and an annotation with @ImportResource("classpath:META-INF/spring/applicationContext.xml") to import some beans I need when processing data in Job.

On the Maven side, I now use some plugins:

  • maven-jar-plugin with <addClasspath>true</addClasspath> and a class containing the main method in the <mainClass>
  • maven-assembly-plugin , because I need a unique jar executable containing all the materials in the dependencies, I use <phase>package</package> to be able to build a jar in the package phase, I also use <goal>single</goal> to be able to build the jar correctly using assembly
  • maven-compiler-plugin indicating that I am using Java 1.7

I think I configured everything that I need to configure, however after Maven BUILD SUCCESS I can not start the task from the command line:

 java -cp ./target/JAR_FILE_NAME.jar org.springframework.batch.core.launch.support.CommandLineJobRunner com.package.bla.bla.ClassContainingTheBatchConfiguration nameOfTheJob 

Throws an IOException due to java.io.FileNotFoundException regarding com.package.bla.bla.ClassContainingTheBatchConfiguration . How to specify options on the command line to complete the task?

+7
source share
3 answers

If you are already using SpringApplication from Spring Boot, why not quit and use @EnableAutoConfiguration , as well as the Maven plugin (see, for example, this guide )? This way you will get something very fast, and you can always add your own functions later.

+2
source

If the first argument to CommandLineJobRunner is your @Configuration FQCN instead of the path to the resource, the ClassPathXmlApplicationContext constructor called from the CommandLineJobRunner start() method will break.

 int start(String jobPath, String jobIdentifier, String[] parameters, Set<String> opts) { ConfigurableApplicationContext context = null; try { context = new ClassPathXmlApplicationContext(jobPath); 

If you have already written a class with main() that replaces CLJR , you should not pass CLJR as the class name on the command line. Pass it on instead.

+1
source

don't use spring.batch.job.enabled = false, then run using java -jar [jar-files] --spring.batch.job.names = [job-name]

0
source

All Articles