我正在构建一个有两个发行版的java服务。每个发行版必须构建不同(一个内部有一个spring-boot嵌入式jetty服务器,另一个没有)。在两者中,我创建了一个具有依赖关系的发行版,除了一个(已经实现)。没有jetty的jar使用maven-assembly-plugin
(与此处Problems running executable jar with dependencies类似)构建,另一个使用spring-boot-maven-plugin
(请参阅http://docs.spring.io/spring-boot/docs/current/maven-plugin/usage.html)。我的问题是外部依赖。
我想在运行时上添加额外的依赖项,但我想要使用OSGi
在两个构建过程中,结果都是可执行的' jar,我可以像这样运行:
java -jar my.jar
但我不知道如何解决其他依赖项。如果我在没有外部依赖项(比如external.jar)的情况下运行上面的命令,my.jar中的内容将会失败,即使它位于同一个文件夹中。我可以让它适用于没有码头的分发:
java -classpath "./*" my.main.App
但这对my-with-jetty.jar不起作用。我也试着跑:
java -classpath "./*" -jar my-with-jetty.jar
这根本不起作用。
所以我的问题是:
是否可以为这两种情况打包一个几乎具有所有依赖关系的jar?
是否可以将jar打包为runnable jar,这样就不必提供主类了?
当然,如果是,怎么样?我想以同样的方式运行两个发行版。
我想要类似的行为:
java -cp "./*" java -jar my.jar conf.cfg
和
java -cp "./*" java -jar my-rest.jar conf.cfg
谢谢。
答案 0 :(得分:0)
您需要至少3个同一父项下的maven项目,以便它们一起构建并具有相同的版本。您的所有代码都将在共享项目中,这将包含在两个可运行的jar项目中。每个可运行的jar项目将以不同的方式构建。
您的父母将会是这样的:
<?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>foo.bar</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>shared-jar</module>
<module>spring-boot-jar</module>
<module>jetty-jar</module>
</modules>
</project>
您的shared-jar项目将拥有所有共享代码
你的spring-boot-jar看起来像:
<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>
<parent>
<groupId>foo.bar</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-jar</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>foo.bar</groupId>
<artifactId>shared-jar</artifactId>
<version>${project.version}</version>
</dependency>
.... you will need to add all the spring boot dependencies with versions
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
您的码头将使用
构建 <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>
<parent>
<groupId>foo.bar</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>jetty-jar</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>foo.bar</groupId>
<artifactId>shared-jar</artifactId>
<version>${project.version}</version>
</dependency>
.... other dependencies
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<!-- <Main-Class>foo.bar.Application</Main-Class> -->
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>