返回

Jar包在Springboot中的使用

前端

构建和发布 JAR 文件:Java 开发指南

简介

JAR(Java 归档)文件是打包和分发 Java 代码、资源和依赖项的一种常用格式。构建和发布 JAR 文件对于 Java 开发人员来说是一项至关重要的技能。本文将指导您完成使用 Maven、Gradle 和直接构建 JAR 文件的过程,并深入了解发布 JAR 文件的不同方法。

构建 JAR 文件

使用 Maven 构建 JAR 文件

Maven 是一个流行的构建工具,它提供了一个简单的机制来构建 JAR 文件。

  1. pom.xml 文件中添加以下依赖项:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-maven-plugin</artifactId>
    <version>2.5.6</version>
</dependency>
  1. pom.xml 文件中添加以下插件:
<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>package</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>
  1. 运行 mvn package 命令构建 JAR 文件。

使用 Gradle 构建 JAR 文件

Gradle 是另一种流行的构建工具,它也提供了构建 JAR 文件的功能。

  1. build.gradle 文件中添加以下依赖项:
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-gradle-plugin:2.5.6'
}
  1. build.gradle 文件中添加以下任务:
task bootJar(type: Jar) {
    mainClassName = 'com.example.demo.DemoApplication'
    from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}
  1. 运行 gradle bootJar 命令构建 JAR 文件。

直接构建 JAR 文件

也可以直接构建 JAR 文件,而无需使用任何构建工具。

  1. 将类文件复制到一个目录中。
  2. 使用以下命令构建 JAR 文件:
jar cvf my-project.jar -C directory .

发布 JAR 文件

一旦构建了 JAR 文件,就可以将其发布以供其他人使用。

使用 Maven 发布 JAR 文件

  1. pom.xml 文件中添加以下分发管理:
<distributionManagement>
    <repository>
        <id>my-repo</id>
        <url>https://example.com/maven/repo</url>
    </repository>
</distributionManagement>
  1. pom.xml 文件中添加以下插件:
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>3.0.0-M3</version>
        <executions>
            <execution>
                <id>deploy</id>
                <phase>deploy</phase>
                <goals>
                    <goal>deploy</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>
  1. 运行 mvn deploy 命令发布 JAR 文件。

使用 Gradle 发布 JAR 文件

  1. build.gradle 文件中添加以下依赖项:
dependencies {
    implementation 'io.spring.gradle:dependency-management-plugin:1.0.11.RELEASE'
}
  1. build.gradle 文件中添加以下插件:
plugins {
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'maven-publish'
}
  1. build.gradle 文件中添加以下发布块:
publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
    repositories {
        maven {
            url 'https://example.com/maven/repo'
        }
    }
}
  1. 运行 gradle publish 命令发布 JAR 文件。

直接发布 JAR 文件

  1. 将 JAR 文件复制到服务器上。
  2. 使用以下命令发布 JAR 文件:
curl -X POST -H "Content-Type: application/java-archive" -d @my-project.jar https://example.com/maven/repo/com/example/my-project/1.0.0/my-project-1.0.0.jar

常见问题解答

1. 如何选择构建和发布 JAR 文件的方法?

这取决于您的项目和偏好。Maven 和 Gradle 都是流行的构建工具,提供了自动化和便利性。直接构建 JAR 文件不需要任何工具,但可能更适合小型项目。

2. 如何配置 JAR 文件中的主类?

使用 Maven 和 Gradle 时,可以在 pom.xmlbuild.gradle 文件中指定主类。直接构建 JAR 文件时,可以使用 -e 选项指定主类,例如 jar cvf my-project.jar -e com.example.demo.DemoApplication -C directory .

3. 如何为 JAR 文件指定依赖项?

Maven 和 Gradle 使用依赖项管理机制。直接构建 JAR 文件时,可以使用 -C 选项包含依赖项 JAR 文件,例如 jar cvf my-project.jar -C directory . -C dependencies/lib .

4. 如何处理 JAR 文件中的资源文件?

Maven 和 Gradle 可以自动处理资源文件。直接构建 JAR 文件时,可以使用 -r 选项指定资源目录,例如 jar cvf my-project.jar -r directory/resources -C directory .

5. 如何签名 JAR 文件?

签名 JAR 文件可以增强安全性。可以使用 jarsigner 工具对 JAR 文件进行签名,例如 jarsigner -keystore mykeystore -storepass mypassword my-project.jar myalias