Home > Mobile >  How to run a specific job using Maven command in Spring Batch?
How to run a specific job using Maven command in Spring Batch?

Time:09-04

Given you have this job configuration below, how can you execute the job scrubWord in command line in a Spring Boot - Spring Batch application using Maven command?

@Configuration
public class WordConfiguration {

    @Autowired
    WordReader wordReader;

    @Autowired
    WordProcessor wordProcessor;

    @Autowired
    WordWriter wordWriter;

    @Bean
    public Job scrubWords() {
        Step scrubWords = stepBuilders
                .get("scrubWord")
                .<Set<String>, Set<String>>chunk(1)
                .reader(wordReader)
                .processor(wordProcessor)
                .writer(wordWriter)
                .build();
        return jobBuilders.get("wordScrubber")
                .start(scrubWords)
                .build();
    }
}

CodePudding user response:

mvn clean spring-boot:run -Dspring.batch.job.names=wordScrubber 
  • clean to overwrite previously compiled files in target/classes.
  • spring-boot:run start ups spring boot.
  • -Dspring.batch.job.names=<jobName> run a specific batch job.

CodePudding user response:

You can also run the executable jar as follows

java -Dspring.batch.job.names=myJob -jar myjar.jar [job parameters]

CodePudding user response:

Solution 1: Inside file application.properties add

spring.batch.job.names=wordScrubber

If you have many jobs

spring.batch.job.names=wordScrubber,foo,bar

then run

mvn spring-boot:run

Solution 2: Best practice way is

$ java -jar myapp.jar --server.port=7070 someParameter=someValue

Not use Maven parameter, use parameter when run JAR file. See reference document at https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.batch.running-from-the-command-line

  • Related