You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在start.sh脚本中动态获取Spring Boot应用的Jar包名?

Dynamic JAR Filename in Spring Boot start.sh Script

Absolutely, you can avoid hardcoding the JAR filename in your start.sh script—shell scripts fully support variable substitution, and there are several reliable approaches to dynamically grab the correct JAR file based on your project setup. Here are the most practical solutions:

1. Shell Pattern Matching (Quickest for Manual Scripts)

If your lib directory only contains one Spring Boot executable JAR (or you can filter out dependency JARs easily), use shell commands to locate the file dynamically.

Example Script:

#!/bin/bash

# Option 1: Match by Spring Boot naming convention (e.g., *spring-boot*.jar)
JAR_FILE=$(ls ../lib/*spring-boot*.jar)

# Option 2: Filter out non-executable JARs (sources, javadocs, dependency JARs)
# JAR_FILE=$(find ../lib -maxdepth 1 -type f -name "*.jar" ! -name "*sources.jar" ! -name "*javadoc.jar" ! -name "*dependency*.jar" | head -n 1)

# Option 3: Match your artifact ID prefix (works if your JAR follows <artifactId>-<version>.jar)
# JAR_FILE=$(ls ../lib/gs-spring-boot-*.jar)

# Start the application
java -jar "$JAR_FILE"

Notes:

  • The ls or find commands will pick up the latest versioned JAR automatically as long as the naming pattern stays consistent.
  • Wrap $JAR_FILE in quotes ("$JAR_FILE") to handle filenames with spaces (though it's best to avoid spaces in project artifacts).

2. Maven-Generated Script (Best for CI/CD Workflows)

For a more robust approach, leverage Maven to generate your start.sh script during the build phase, injecting the correct artifact ID and version directly from your pom.xml.

Step 1: Create a Script Template

Make a start.sh.template file in your src/main/resources directory:

#!/bin/bash
java -jar ../lib/@project.artifactId@-@project.version@.jar

Step 2: Configure Maven Resources Plugin

Add this to your pom.xml to enable filtering and generate the final script during the package phase:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>start.sh.template</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.3.1</version>
            <executions>
                <execution>
                    <id>generate-start-script</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <filtering>true</filtering>
                                <includes>
                                    <include>start.sh.template</include>
                                </includes>
                            </resource>
                        </resources>
                        <destFileName>start.sh</destFileName>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- Optional: Ensure your Spring Boot JAR is packaged into the lib directory -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
            </configuration>
        </plugin>
    </plugins>
</build>

How It Works:

Maven replaces @project.artifactId@ and @project.version@ with the values from your pom.xml, generating a ready-to-use start.sh in your build output directory. This eliminates any manual updates when your version changes.

3. Fetch Version via Maven (Requires Maven in Deployment Env)

If your deployment environment has Maven installed, you can pull the version directly from your pom.xml in the script:

#!/bin/bash
ARTIFACT_ID="gs-spring-boot"
VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
JAR_FILE="../lib/${ARTIFACT_ID}-${VERSION}.jar"

java -jar "$JAR_FILE"

This is less common since most production environments don't have Maven installed, but it's useful for testing or staging setups.


内容的提问来源于stack exchange,提问作者PPB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:08:51