Make sample to work with spring-native

- While project uses jdk8, you can do native build with jdk11
  `./mvnw clean package -Pnative`
- You can then run these
  spring-shell-samples/target/spring-shell-samples
  java -jar spring-shell-samples/target/spring-shell-samples-3.0.0-SNAPSHOT-exec.jar
- Remove logback.xml in favour of spring config to workaround for
  https://github.com/spring-projects-experimental/spring-native/issues/625
- For now remove jcommander command sample to get support for it #340
- Relates #323
This commit is contained in:
Janne Valkealahti
2021-12-24 13:25:07 +00:00
parent d879b7d6c4
commit 54702460d1
7 changed files with 90 additions and 168 deletions

View File

@@ -19,14 +19,15 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>org.springframework.shell.samples.SpringShellSample</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.shell</groupId>
<artifactId>spring-shell-starter</artifactId>
@@ -36,4 +37,85 @@
<artifactId>jcommander</artifactId>
</dependency>
</dependencies>
<profiles>
<profile>
<id>native</id>
<!-- needs GRAALVM_HOME to point to graal tools -->
<properties>
<spring-native.version>0.11.1</spring-native.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-native</artifactId>
<version>${spring-native.version}</version>
</dependency>
<!-- Required with Maven Surefire 2.x -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-aot-maven-plugin</artifactId>
<version>${spring-native.version}</version>
<executions>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<version>0.9.9</version>
<extensions>true</extensions>
<executions>
<execution>
<id>build-native</id>
<goals>
<goal>build</goal>
</goals>
<phase>package</phase>
</execution>
<execution>
<id>test-native</id>
<goals>
<goal>test</goal>
</goals>
<phase>test</phase>
</execution>
</executions>
<configuration>
<!-- ... -->
</configuration>
</plugin>
<!-- Avoid a clash between Spring Boot repackaging and native-maven-plugin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releasess</name>
<url>https://repo.spring.io/release</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
</project>

View File

@@ -6,7 +6,7 @@
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -34,7 +34,7 @@ import org.springframework.shell.Shell;
import org.springframework.shell.jline.InteractiveShellApplicationRunner;
import org.springframework.util.StringUtils;
@Configuration
@Configuration(proxyBeanMethods = false)
public class ExampleApplicationRunnerConfiguration {
@Autowired

View File

@@ -1,55 +0,0 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.samples.jcommander;
import java.util.ArrayList;
import java.util.List;
import com.beust.jcommander.Parameter;
import javax.validation.constraints.Min;
/**
* An example straight from the JCommander documentation.
*
* @author Eric Bottard
* @author Cédric Beust
*/
public class Args {
@Parameter
private List<String> parameters = new ArrayList<>();
@Min(3)
@Parameter(names = { "-log", "-verbose" }, description = "Level of verbosity")
private Integer verbose = 1;
@Parameter(names = "-groups", description = "Comma-separated list of group names to be run")
private String groups;
@Parameter(names = "-debug", description = "Debug mode")
private boolean debug = false;
@Override
public String toString() {
return "Args{" +
"parameters=" + parameters +
", verbose=" + verbose +
", groups='" + groups + '\'' +
", debug=" + debug +
'}';
}
}

View File

@@ -1,37 +0,0 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.samples.jcommander;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellOption;
import javax.validation.Valid;
/**
* A class with JCommander commands.
*
* @author Eric Bottard
*/
@ShellComponent
public class JCommanderCommands {
@ShellMethod("Bind parameters to JCommander POJO.")
public String jcommander(@ShellOption(optOut = true) @Valid Args args) {
return "You said " + args;
}
}

View File

@@ -1,63 +0,0 @@
/*
* Copyright 2017-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.samples.noautoconf;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.shell.boot.JCommanderParameterResolverAutoConfiguration;
import org.springframework.shell.boot.JLineShellAutoConfiguration;
import org.springframework.shell.boot.SpringShellAutoConfiguration;
import org.springframework.shell.boot.StandardAPIAutoConfiguration;
import org.springframework.shell.boot.StandardCommandsAutoConfiguration;
import org.springframework.shell.samples.jcommander.JCommanderCommands;
import org.springframework.shell.samples.standard.Commands;
import org.springframework.shell.samples.standard.DynamicCommands;
import org.springframework.shell.samples.standard.TableCommands;
import org.springframework.shell.standard.FileValueProvider;
/**
* This class shows how to use the full extent of Spring Shell without relying on Boot auto configuration.
*
* @author Eric Bottard
*/
@Configuration
@Import({
// Core runtime
SpringShellAutoConfiguration.class,
JLineShellAutoConfiguration.class,
// Various Resolvers
JCommanderParameterResolverAutoConfiguration.class,
StandardAPIAutoConfiguration.class,
// Built-In Commands
StandardCommandsAutoConfiguration.class,
// Allows ${} support
//PropertyPlaceholderAutoConfiguration.class,
// Sample Commands
JCommanderCommands.class,
Commands.class,
FileValueProvider.class,
DynamicCommands.class,
TableCommands.class,
})
public class NoAutoConf {
public static void main(String[] args) {
SpringApplication.run(NoAutoConf.class, args);
}
}

View File

@@ -1,3 +1,3 @@
spring:
main:
allow-circular-references: false
logging:
level:
root: error

View File

@@ -1,5 +0,0 @@
<configuration>
<!-- Disable most stdout logging by default -->
<root level="ERROR">
</root>
</configuration>