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

如何通过Gradle/Maven为Windows/Linux构建对应平台专属依赖的Jar

针对不同平台构建专属Jar并引入对应依赖的实现方案

Gradle 实现方式

Gradle支持通过自定义配置和构建脚本逻辑,轻松实现平台专属依赖的引入和打包。

1. 自动根据当前操作系统选择依赖

在build.gradle中定义平台专属的依赖配置,并让Jar任务根据当前系统自动选择对应的依赖打包:

// 定义继承自implementation的平台专属配置
configurations {
    windowsImplementation.extendsFrom implementation
    linuxImplementation.extendsFrom implementation
}

dependencies {
    // 通用依赖,所有平台都会引入
    implementation 'com.example:common:1.0.0'
    
    // Windows专属依赖
    windowsImplementation 'com.example:windows-specific:1.0.0'
    
    // Linux专属依赖
    linuxImplementation 'com.example:linux-specific:1.0.0'
}

jar {
    def currentOs = org.gradle.nativeplatform.platform.internal.DefaultNativePlatform.currentOperatingSystem
    // 根据当前操作系统选择对应的依赖集合
    def targetConfig = currentOs.windows ? configurations.windowsImplementation : configurations.linuxImplementation
    // 将依赖的class文件打包进Jar
    from targetConfig.collect { it.isDirectory() ? it : zipTree(it) }
}

执行./gradlew jar时,Gradle会自动识别当前操作系统,打包对应平台的依赖。

2. 手动指定目标平台构建

如果需要在任意系统上构建指定平台的Jar,可以通过命令行参数手动指定:

// 从命令行参数获取目标平台,默认使用当前系统
def targetPlatform = project.findProperty('targetPlatform') ?: 
    org.gradle.nativeplatform.platform.internal.DefaultNativePlatform.currentOperatingSystem.name.toLowerCase()

configurations {
    windowsImplementation.extendsFrom implementation
    linuxImplementation.extendsFrom implementation
}

dependencies {
    implementation 'com.example:common:1.0.0'
    windowsImplementation 'com.example:windows-specific:1.0.0'
    linuxImplementation 'com.example:linux-specific:1.0.0'
}

jar {
    // 根据指定平台选择依赖配置
    from configurations["${targetPlatform}Implementation"].collect { it.isDirectory() ? it : zipTree(it) }
}

构建Windows平台Jar:

./gradlew jar -PtargetPlatform=windows

构建Linux平台Jar:

./gradlew jar -PtargetPlatform=linux

Maven 实现方式

Maven通过Profile机制实现不同环境/平台的依赖切换,支持自动激活或手动指定。

1. 定义平台专属Profile

在pom.xml中添加针对Windows和Linux的Profile,配置专属依赖,并设置自动激活条件:

<dependencies>
    <!-- 通用依赖,所有平台共享 -->
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>common</artifactId>
        <version>1.0.0</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

<profiles>
    <!-- Windows平台Profile,在Windows系统上自动激活 -->
    <profile>
        <id>windows</id>
        <activation>
            <os>
                <family>windows</family>
            </os>
        </activation>
        <dependencies>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>windows-specific</artifactId>
                <version>1.0.0</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
    </profile>

    <!-- Linux平台Profile,在Linux系统上自动激活 -->
    <profile>
        <id>linux</id>
        <activation>
            <os>
                <family>unix</family>
                <name>linux</name>
            </os>
        </activation>
        <dependencies>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>linux-specific</artifactId>
                <version>1.0.0</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
    </profile>
</profiles>

2. 打包包含依赖的Jar

Maven默认Jar仅包含项目自身代码,如需将依赖打包进Jar,需使用maven-shade-plugin或maven-assembly-plugin。以下是maven-shade-plugin的配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.4.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

3. 构建命令

  • 自动识别平台构建:直接执行mvn package,Maven会根据当前系统激活对应Profile。
  • 手动指定平台构建:
    构建Windows平台Jar:
    mvn package -Pwindows
    
    构建Linux平台Jar:
    mvn package -Plinux
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 05:33:29