【发布时间】:2018-11-06 06:23:01
【问题描述】:
几天来一直在努力解决这个问题,希望得到一些指导。
我正在使用 Spring Boot,并试图将我的项目构建到一个可以运行的 jar 中。有一些嵌套依赖项 (OpenCV) 需要我将它们全部包含到一个 Jar 文件中。
在添加这些依赖项之前,我能够成功构建一个运行良好的 Jar 文件;直到我开始尝试构建一个组合罐,事情才开始成为一个问题。
我面临的问题是:我如何成功构建一个包含所有 Spring 文件、嵌套依赖项和我的源代码的 Jar 文件到一个胖 Jar 文件中? p>
下面是我的 POM 文件,省略了一些依赖项。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myapp</groupId>
<artifactId>mytestproject</artifactId>
<packaging>jar</packaging>
<!--Change the above to a war instead of jar if you need a war file-->
<name>Spring OSGi Bundle</name>
<version>0.1</version>
<url>http://www.springframework.org/osgi</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<properties>
<spring.osgi.version>1.2.1</spring.osgi.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>
<!-- ================================================ -->
<!-- Repository Configuration -->
<!-- ================================================ -->
<!-- Repos -->
<repositories>
<!-- Apache imaging https://commons.apache.org/proper/commons-imaging/index.html -->
<repository>
<id>apache.snapshots</id>
<name>Apache Development Snapshot Repository</name>
<url>https://repository.apache.org/content/repositories/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>i21-s3-osgi-repo</id>
<name>i21 osgi artifacts repo</name>
<snapshots>
<enabled>true</enabled>
</snapshots>
<url>http://maven.springframework.org/osgi</url>
</repository>
<repository>
<id>com.springsource.repository.bundles.external</id>
<name>SpringSource Enterprise Bundle Repository - External Bundle Releases</name>
<url>http://repository.springsource.com/maven/bundles/external</url>
</repository>
<repository>
<id>com.springsource.repository.bundles.release</id>
<name>SpringSource Enterprise Bundle Repository - SpringSource Bundle Releases</name>
<url>http://repository.springsource.com/maven/bundles/release</url>
</repository>
<repository>
<id>com.springsource.repository.bundles.milestone</id>
<name>SpringSource Enterprise Bundle Repository - SpringSource Milestone Releases</name>
<url>http://repository.springsource.com/maven/bundles/milestone</url>
</repository>
<repository>
<id>spring-release</id>
<name>Spring Portfolio Release Repository</name>
<url>http://maven.springframework.org/release</url>
</repository>
<repository>
<id>eclipse-repository</id>
<name>Eclipse Repository</name>
<url>http://repo1.maven.org/eclipse/</url>
</repository>
<repository>
<id>spring-ext</id>
<name>Spring External Dependencies Repository</name>
<url>
https://springframework.svn.sourceforge.net/svnroot/springframework/repos/repo-ext/
</url>
</repository>
</repositories>
<!-- Plugin Repos -->
<pluginRepositories>
<pluginRepository>
<id>maven-repo</id>
<name>maven repo</name>
<url>http://repo1.maven.org/maven2/</url>
</pluginRepository>
<pluginRepository>
<id>com.springsource.repository.bundles.milestone</id>
<name>SpringSource Enterprise Bundle Repository - SpringSource Milestone Releases</name>
<url>http://repository.springsource.com/maven/bundles/milestone</url>
</pluginRepository>
</pluginRepositories>
<!-- Dependencies -->
<dependencies>
<!-- Local Jar File for open.cv -->
<dependency>
<groupId>org.openpnp</groupId>
<artifactId>opencv</artifactId>
<scope>system</scope>
<version>3.2.0</version>
<systemPath>${basedir}/src/main/libs/opencv-320.jar</systemPath>
</dependency>
<!-- OSGI -->
<dependency>
<groupId>org.springframework.osgi</groupId>
<artifactId>spring-osgi-test</artifactId>
<version>${spring.osgi.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.osgi</groupId>
<artifactId>spring-osgi-annotation</artifactId>
<version>${spring.osgi.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Build Tag -->
<build>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<resources>
<!-- standard Maven folder -->
<resource>
<directory>src/main/resources</directory>
</resource>
<!-- plus root folder -->
<resource>
<directory>.</directory>
<includes>
<include>plugin.xml</include>
<include>META-INF/*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--<classifier>sources</classifier>-->
<!--<classifier>spring-boot</classifier>-->
<mainClass>com.myapp.internal.MainLauncher</mainClass>
<addResources>true</addResources>
<!--<executable>true</executable>-->
</configuration>
<executions>
<execution>
<id>attach-sources</id>
<!--<phase>compile</phase>-->
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<!--<classpath>.</classpath>-->
<addClasspath>true</addClasspath>
<mainClass>com.myapp.internal.MainLauncher</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<!--<execution>-->
<!--<id>attach-sources</id>-->
<!--<phase>compile</phase>-->
<!--<goals>-->
<!--<goal>single</goal>-->
<!--</goals>-->
<!--</execution>-->
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我试图在 Intellij IDE 的终端上编译 3 个不同的命令:
1) mvn 清洁包
2) mvn 全新安装
3) mvn clean compile assembly:single
这 3 个命令加上更改构建标记中的注释代码总是会产生以下两种结果之一:
要么我得到一个包含所有内容的大 jar 文件除了我的源代码不在基本目录中(它位于 BOOT-INF 目录下,运行时找不到) 并且每当我使用 java -jar {MyJar}.jar 运行它时都会收到标准的“找不到 MainLauncher”错误。
或
我得到 2 个 jar 文件,其中“jar-with-dependencies”版本包含我的源代码以及其他库,但其中没有 Spring 库。
因此,这两个罐子都不起作用,因为我需要将它们两者结合起来。
到目前为止我尝试过的事情:
1) Packaging spring sub-projects into one jar
2) No source jar attached with maven-assembly-plugin
3)Include spring xml in jar by maven-assembly-plugin packaging
4) How do I create an an executable jar in Spring Boot?
5)https://www.mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/
为了实现一个 jar 文件与我的源文件、spring 文件和嵌套依赖项的目标,我缺少什么?
【问题讨论】:
-
你真的需要 pom 中的所有东西吗?你能在 github 上发布一个最小的、可验证的样本吗?
-
我宁愿站在太多信息的一边,而不是太少。可能是我误认为其他人很容易看到的简单内容。
-
你能用
org.springframework.boot spring-boot-maven-plugin 替换你的构建部分吗 -
并使用命令 mvn clean install
-
同时从你的依赖中移除系统范围。
标签: java spring maven spring-boot jar