1

I have an existing application which is a jetty app written in Java and managed with maven. There are a few basic tasks that can be run from the browser but mainly it runs all the tasks in one go when run from the command line (or Jenkins).

It does this by calling a specific Maven profile

so the maven command line call is

mvn clean install -P autorun

The autorun profile is

    <profile>
        <id>autorun</id>
        <build>
          <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.8</version>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <configuration>
                          <target>
                            <property name="runtime_classpath" refid="maven.runtime.classpath"/>
                            <java classname="com.app.tool.Client" >
                                <arg value="-h" />                                  
                                <classpath>
                                    <pathelement path="${runtime_classpath}"/>
                                </classpath>
                                </java>
                          </target>
                        </configuration>
                        <goals>
                          <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
              </plugin>
            </plugins>  
        </build>
    </profile>

there is a main procedure in the Client class

public static void main(String... args) throws Exception { ...

which essentially

  1. creates an org.eclipse.jetty.server.Server object
  2. uses the war file to kick off the http server app
  3. Uses a number of org.springframework.http.client.ClientHttpRequest objects to fire off a number of tasks using code along the lines of

    ClientHttpRequest request = restTemplate
            .getRequestFactory()
            .createRequest(
                    new URI("http://localhost:8080/tool/spring/" + jobName),
                    HttpMethod.GET);
    ClientHttpResponse clientHttpResponse = request.execute();
    if (clientHttpResponse.getStatusCode().value() != HttpStatus.OK_200) {
        throw new RuntimeException(...);
    }
    

I am going to refactor the app now for various reasons. My aim is to keep the current design as, although it's main current use points more to a console app, I would like to make add some more useful interactive functionality.

So to finally get to the point, I have started re-writing the app as a Spring Boot application. I would like to still be able to fire off the jobs I want from the command line (and Jenkins). Is there a simple (& equivalent) way to do this with Spring Boot & Maven ? I am using Main to fire off the app so possibly I need to pass a switch into here

public static void main(String[] args) throws Exception {
    SpringApplication.run(SpringBootStartUpConfig.class, args);
}

I'm guessing that a lot of the code currently used to start the server can be dumped but I'm struggling to find the best way to achieve this ? (I'd prefer to use Maven as this is the core tool we are using but I can use Gradle if this is something that it is better at).

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
gringogordo
  • 1,990
  • 6
  • 24
  • 60
  • and why would issuing the requests be different? When using maven you can just use `mvn springBoot:run` to launch the application instead of a profile or anything else. – M. Deinum Jul 20 '17 at 09:15

1 Answers1

0

To run a spring boot application from command line you could include the spring boot maven plugin see reference here Spring boot maven plugin

and then launch it from command line using

mvn spring-boot:run
Mukul Goel
  • 8,387
  • 6
  • 37
  • 77
  • 1
    Oh yes, actually I was already doing that, I guess this answer (combined with yours) gets me where I wanted to get. Sorry feeling a bit silly now... https://stackoverflow.com/questions/23316843/get-command-line-arguments-from-spring-bootrun – gringogordo Jul 20 '17 at 10:12
  • 1
    @gringogordo: i suggest you look into spring-profiles to create profiles and then you can pass in profile names when starting in the app instead of passing everything as command line arguments. Not exactly sure you need this but have a look – Mukul Goel Jul 20 '17 at 11:30