diff --git a/spring-bootstrap-cli/samples/job.groovy b/spring-bootstrap-cli/samples/job.groovy new file mode 100644 index 0000000000..6c917d764b --- /dev/null +++ b/spring-bootstrap-cli/samples/job.groovy @@ -0,0 +1,88 @@ +package org.test + +@GrabResolver(name='spring-milestone', root='http://repo.springframework.org/milestone') +@Grab("org.hsqldb:hsqldb-j5:2.0.0") +@Configuration +@EnableBatchProcessing +class JobConfig { + + @Autowired + private JobBuilderFactory jobs + + @Autowired + private StepBuilderFactory steps + + @Bean + protected Tasklet tasklet() { + return new SampleTasklet() + } + + @Bean + Job job() throws Exception { + return jobs.get("job").start(step1()).build() + } + + @Bean + protected Step step1() throws Exception { + return steps.get("step1").tasklet(tasklet()).build() + } +} + +class SampleTasklet implements Tasklet { + @Override + RepeatStatus execute(StepContribution contribution, ChunkContext context) { + return RepeatStatus.FINISHED + } +} + +import org.springframework.util.StringUtils +import groovy.util.logging.Log + +@Component +@Log +class JobRunner implements CommandLineRunner { + + @Autowired(required=false) + private JobParametersConverter converter = new DefaultJobParametersConverter() + + @Autowired + private JobLauncher jobLauncher + + @Autowired + private Job job + + void run(String... args) { + log.info("Running default command line with: ${args}") + launchJobFromProperties(StringUtils.splitArrayElementsIntoProperties(args, "=")) + } + + protected void launchJobFromProperties(Properties properties) { + jobLauncher.run(job, converter.getJobParameters(properties)) + } +} + +import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils +import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator + +@Component +class DatabaseInitializer { + + @Autowired + private DataSource dataSource + + @Autowired + private ResourceLoader resourceLoader + + @PostConstruct + protected void initialize() { + String platform = org.springframework.batch.support.DatabaseType.fromMetaData(dataSource).toString().toLowerCase() + if (platform=="hsql") { + platform = "hsqldb" + } + ResourceDatabasePopulator populator = new ResourceDatabasePopulator() + populator.addScript(resourceLoader.getResource("org/springframework/batch/core/schema-${platform}.sql")) + populator.setContinueOnError(true) + DatabasePopulatorUtils.execute(populator, dataSource) + } +} + diff --git a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/GroovyCompiler.java b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/GroovyCompiler.java index bfd76d94c9..b553956c27 100644 --- a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/GroovyCompiler.java +++ b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/GroovyCompiler.java @@ -31,6 +31,7 @@ import org.codehaus.groovy.control.CompilerConfiguration; import org.codehaus.groovy.control.SourceUnit; import org.codehaus.groovy.control.customizers.CompilationCustomizer; import org.codehaus.groovy.control.customizers.ImportCustomizer; +import org.springframework.bootstrap.cli.compiler.autoconfigure.SpringBatchCompilerAutoConfiguration; import org.springframework.bootstrap.cli.compiler.autoconfigure.SpringBootstrapCompilerAutoConfiguration; import org.springframework.bootstrap.cli.compiler.autoconfigure.SpringMvcCompilerAutoConfiguration; @@ -54,6 +55,7 @@ public class GroovyCompiler { private static final CompilerAutoConfiguration[] COMPILER_AUTO_CONFIGURATIONS = { new SpringBootstrapCompilerAutoConfiguration(), new SpringMvcCompilerAutoConfiguration(), + new SpringBatchCompilerAutoConfiguration(), new SpringBootstrapCompilerAutoConfiguration() }; private GroovyCompilerConfiguration configuration; diff --git a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBatchCompilerAutoConfiguration.java b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBatchCompilerAutoConfiguration.java index 54aaa9cc89..1dd8d43d9e 100644 --- a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBatchCompilerAutoConfiguration.java +++ b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBatchCompilerAutoConfiguration.java @@ -38,7 +38,9 @@ public class SpringBatchCompilerAutoConfiguration extends CompilerAutoConfigurat @Override public void applyDependencies(DependencyCustomizer dependencies) { dependencies.ifAnyMissingClasses("org.springframework.batch.core.Job").add( - "org.springframework.batch", "spring-batch-core", "2.1.9.RELEASE"); + "org.springframework.batch", "spring-batch-core", "2.2.0.RC1"); + dependencies.ifAnyMissingClasses("org.springframework.jdbc.core.JdbcTemplate") + .add("org.springframework", "spring-jdbc", "4.0.0.BOOTSTRAP-SNAPSHOT"); } @Override @@ -59,6 +61,7 @@ public class SpringBatchCompilerAutoConfiguration extends CompilerAutoConfigurat "org.springframework.batch.core.JobParameter", "org.springframework.batch.core.JobParameters", "org.springframework.batch.core.launch.JobLauncher", + "org.springframework.batch.core.converter.JobParametersConverter", "org.springframework.batch.core.converter.DefaultJobParametersConverter"); } } diff --git a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBootstrapCompilerAutoConfiguration.java b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBootstrapCompilerAutoConfiguration.java index 8dc1dcf0a1..aeb5af56b1 100644 --- a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBootstrapCompilerAutoConfiguration.java +++ b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBootstrapCompilerAutoConfiguration.java @@ -55,7 +55,8 @@ public class SpringBootstrapCompilerAutoConfiguration extends CompilerAutoConfig @Override public void applyImports(ImportCustomizer imports) { - imports.addImports("javax.sql.DataSource", + imports.addImports("javax.sql.DataSource", "javax.annotation.PostConstruct", + "javax.annotation.PreDestroy", "org.springframework.stereotype.Controller", "org.springframework.stereotype.Service", "org.springframework.stereotype.Component", @@ -67,6 +68,8 @@ public class SpringBootstrapCompilerAutoConfiguration extends CompilerAutoConfig "org.springframework.context.annotation.Scope", "org.springframework.context.annotation.Configuration", "org.springframework.context.annotation.Bean", + "org.springframework.core.io.ResourceLoader", + "org.springframework.bootstrap.CommandLineRunner", "org.springframework.bootstrap.context.annotation.EnableAutoConfiguration"); imports.addStarImports("org.springframework.stereotype"); }