Nikhil Pareek
Nikhil Pareek

Reputation: 764

how to launch Spring Batch Job using CommandLineJobRunner having Java configuration

I have my batch job definition in Java based configuration file. I have seen that CommandLineJobRunner can be used to launch job, but the job definition should be defined in .xml. I want to use CommandLineJobRunner to run my jobs defined in java based configuration.

According to the documentation here: https://docs.spring.io/spring-batch/trunk/reference/html/configureJob.html#commandLineJobRunner, there is no details to do so.

Can we even do this? What can be the other alternatives?

Upvotes: 6

Views: 12130

Answers (2)

Ganesh I
Ganesh I

Reputation: 1

cd to your project, get all dependencies using

mvn dependency:copy-dependencies

In your maven project's pom.xml build->plugins secion add follwing

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>org.springframework.batch.core.launch.support.CommandLineJobRunner</mainClass>
            </configuration>   
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
              <archive>
                <manifest>
                  <addClasspath>true</addClasspath>
                  <mainClass>org.springframework.batch.core.launch.support.CommandLineJobRunner</mainClass>
                </manifest>
              </archive>
            </configuration>
      </plugin>

Create project jar using your build method and run from command line.

java -cp dependency/* -jar your.jar com.abc.BatchConfigurationA JobA -- parm1=valA,java.lang.String,false parm2=22,java.lang.Long,false

Upvotes: 0

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31590

The first argument of CommandLineJobRunner can be either:

  • The xml file containing the job definition
  • Or the fully qualified name of the configuration class containing the job definition

Starting from Spring Batch v4, there is a toggle on the top of each documentation page that allows you to show examples in Java or Xml config. For example, when the toggle is set to "Java", the documentation section here: https://docs.spring.io/spring-batch/4.0.x/reference/html/job.html#runningJobsFromCommandLine shows how to use the CommandLineJobRunner with a Java configuration class:

$>java -cp your/class/path org.springframework.batch.core.launch.support.CommandLineJobRunner io.spring.EndOfDayJobConfiguration endOfDay schedule.date(date)=2007/05/05

io.spring.EndOfDayJobConfiguration is the fully qualified class name containing the endOfDay job definition.

Upvotes: 2

Related Questions