Restored version command result as in v3 (#1286)#1298
Restored version command result as in v3 (#1286)#1298fmbenhassine merged 1 commit intospring-projects:mainfrom
Conversation
|
I've created records |
|
LGTM! However, this will only work with Spring Boot right? I am thinking about non boot users. Moreover, I can't seem to get build and git details when testing the change set. Here are the changes I tried on the Boot sample: Index: spring-shell-samples/spring-shell-sample-spring-boot/pom.xml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/spring-shell-samples/spring-shell-sample-spring-boot/pom.xml b/spring-shell-samples/spring-shell-sample-spring-boot/pom.xml
--- a/spring-shell-samples/spring-shell-sample-spring-boot/pom.xml (revision e7a56eedeec72f95fd14b677e3d8fc9b355ea008)
+++ b/spring-shell-samples/spring-shell-sample-spring-boot/pom.xml (date 1769592876333)
@@ -26,4 +26,24 @@
</dependency>
</dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-maven-plugin</artifactId>
+ <executions>
+ <execution>
+ <phase>package</phase>
+ <goals>
+ <goal>repackage</goal>
+ </goals>
+ <configuration>
+ <mainClass>org.springframework.shell.samples.helloworld.boot.SpringShellApplication</mainClass>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
</project>
Index: spring-shell-samples/spring-shell-sample-spring-boot/src/main/resources/application.properties
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>ISO-8859-1
===================================================================
diff --git a/spring-shell-samples/spring-shell-sample-spring-boot/src/main/resources/application.properties b/spring-shell-samples/spring-shell-sample-spring-boot/src/main/resources/application.properties
--- a/spring-shell-samples/spring-shell-sample-spring-boot/src/main/resources/application.properties (revision e7a56eedeec72f95fd14b677e3d8fc9b355ea008)
+++ b/spring-shell-samples/spring-shell-sample-spring-boot/src/main/resources/application.properties (date 1769593001068)
@@ -1,1 +1,11 @@
-spring.application.name=spring-shell-hello-world
\ No newline at end of file
+spring.application.name=spring-shell-hello-world
+spring.shell.command.version.show-build-artifact=true
+spring.shell.command.version.show-build-group=true
+spring.shell.command.version.show-build-version=true
+spring.shell.command.version.show-build-time=true
+spring.shell.command.version.show-build-name=true
+
+spring.shell.command.version.show-git-branch=true
+spring.shell.command.version.show-git-commit-time=true
+spring.shell.command.version.show-git-commit-id=true
+spring.shell.command.version.show-git-short-commit-id=true
\ No newline at end of file
Building a jar file and running it with |
|
Hello @fmbenhassine, <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>org.springframework.shell.samples.helloworld.boot.SpringShellApplication</mainClass>
</configuration>
</execution>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
<version>9.0.2</version>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
<configuration>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>This will generate
|
|
Ah, yeah... there is an example |
|
For non-Spring users, this can be done in a similar way by adding the required plugins to their build tool configuration, for example: <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>org.springframework.shell.samples.helloworld.SpringShellApplication</mainClass>
</configuration>
</execution>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
<version>9.0.2</version>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
<configuration>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>This would then require implementing something like the following in private Version createVersion() {
Version version = new Version();
version.setShowBuildVersion(true);
version.setShowBuildTime(true);
version.setShowGitCommitId(true);
version.setShowGitShortCommitId(true);
version.setShowGitCommitTime(true);
version.setShowGitBranch(true);
version.setShowBuildGroup(true);
version.setShowBuildArtifact(true);
version.setShowBuildName(true);
version.setBuildProperties(new Version.BuildProperties("org.springframework.shell", "spring-shell-core",
"Spring Shell", this.getClass().getPackage().getImplementationVersion(), Instant.now()));
version.setGitProperties(new Version.GitProperties("main", "abcdef1234567890", "abcdef1", Instant.now()));
return version;
}In the I’m not sure this replication is the right approach, as it effectively duplicates existing code. As mentioned earlier, this could be done as a follow-up, and at least provide build information for Spring Boot users, who I believe will be the majority. |
|
Also this approach is for non-boot users possible: package org.springframework.shell.samples.helloworld;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.shell.core.ShellRunner;
import org.springframework.shell.core.command.Version;
import org.springframework.shell.core.command.annotation.Command;
import org.springframework.shell.core.command.annotation.EnableCommand;
import org.springframework.shell.core.command.annotation.Option;
import java.time.Instant;
@EnableCommand(SpringShellApplication.class)
public class SpringShellApplication implements InitializingBean, ApplicationContextAware {
public static void main(String[] args) throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringShellApplication.class);
ShellRunner runner = context.getBean(ShellRunner.class);
runner.run(args);
}
@Command(name = "hello", description = "Say hello to a given name", group = "Greetings",
help = "A command that greets the user with 'Hello ${name}!'. Usage: hello [-n | --name]=<name>")
public void sayHello(@Option(shortName = 'n', longName = "name", description = "the name of the person to greet",
defaultValue = "World") String name) {
System.out.println("Hello " + name + "!");
}
@Override
public void afterPropertiesSet() throws Exception {
Version version = applicationContext.getBean(Version.class);
version.setShowBuildVersion(true);
version.setShowBuildTime(true);
version.setShowGitCommitId(true);
version.setShowGitShortCommitId(true);
version.setShowGitCommitTime(true);
version.setShowGitBranch(true);
version.setShowBuildGroup(true);
version.setShowBuildArtifact(true);
version.setShowBuildName(true);
version.setBuildProperties(new Version.BuildProperties("org.springframework.shell", "spring-shell-core",
"Spring Shell", this.getClass().getPackage().getImplementationVersion(), Instant.now()));
version.setGitProperties(new Version.GitProperties("main", "abcdef1234567890", "abcdef1", Instant.now()));
}
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
} |
|
Thank you for your feedback about these plugins configuration @czpilar ! I was sure I was missing something 😂
Ideally, all features should be the same in both worlds (boot and non-boot) without having two versions of the same command. The added value of Boot would be auto-configuring things. The fact that it's difficult to achieve this easily shows how tied v3 was to Boot. I really struggled with this, see #1207 and #1206. Anyway, I am not keen to merge a "feature" that works only for Boot, so we need to figure out how to make this work in both worlds. The good thing is that you introduced two new types: In a non-boot world, the user has to provide them manually (like any other config). In a Boot world, those beans would be provided automatically. With that in place, we would have the same |
|
@fmbenhassine fully understand... I have now some idea. Will look at this later... |
Signed-off-by: David Pilar <david@czpilar.net>
ab155b7 to
1205290
Compare
|
Now it is complete and works for both Boot and non-Boot users. Non-Boot users just need to register Spring beans for package org.springframework.shell.samples.helloworld;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.shell.core.ShellRunner;
import org.springframework.shell.core.command.Version;
import org.springframework.shell.core.command.annotation.Command;
import org.springframework.shell.core.command.annotation.EnableCommand;
import org.springframework.shell.core.command.annotation.Option;
import java.time.Instant;
@EnableCommand(SpringShellApplication.class)
public class SpringShellApplication {
public static void main(String[] args) throws Exception {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringShellApplication.class);
ShellRunner runner = context.getBean(ShellRunner.class);
runner.run(args);
}
@Command(name = "hello", description = "Say hello to a given name", group = "Greetings",
help = "A command that greets the user with 'Hello ${name}!'. Usage: hello [-n | --name]=<name>")
public void sayHello(@Option(shortName = 'n', longName = "name", description = "the name of the person to greet",
defaultValue = "World") String name) {
System.out.println("Hello " + name + "!");
}
@Bean
public Version.BuildProperties versionBuildProperties() {
return new Version.BuildProperties("com.example", "spring-shell-sample", "Spring Shell Sample", "1.0.0",
Instant.now());
}
@Bean
public Version.GitProperties versionGitProperties() {
return new Version.GitProperties("main", "abc1234", "abc1234", Instant.now());
}
}And that's it. Of course, boot-users can also register those beans which will override autoconfigured settings. Screenshot for boot users:
Screenshot for non-boot users:
Please have a look @fmbenhassine |
|
Fantastic! This is exactly what I meant here #1298 (comment). I can't thank you enough for all your help on Spring Shell 🙏 |



Resolves #1286