如何使用嵌入式码头和 Maven 构建版本?
How can I build a release with embedded jetty and maven?
我正在尝试配置 Maven 来构建一个 运行nable jar。
这是关于的项目包含几个依赖项以及嵌入式码头。
我想嵌入所有依赖项(有效)并拥有一个可执行 jar。
我可以从 eclipse 运行 项目正常,但是一旦我尝试 运行 罐子,我在 org.eclipse.jetty.servlet.ServletContextHandler.
上得到 ClassNotFoundExceptions
但是,当我使用 maven-plugin 和 运行 exec:java 程序启动时没有问题。
我做错了什么?
这是我的副本 pom.xml
<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>comms</groupId>
<artifactId>comms-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Air</name>
<properties>
<jettyVersion>9.2.7.v20150116</jettyVersion>
<gsonVersion>2.3.1</gsonVersion>
<logjVersion>2.1</logjVersion>
<lombokVersion>1.16.2</lombokVersion>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jettyVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gsonVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${logjVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${logjVersion}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombokVersion}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals><goal>java</goal></goals>
</execution>
</executions>
<configuration>
<mainClass>comms.Loader</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<mainClass>comms.Loader</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
我注意到您将 jetty-maven-plugin 声明为 dependency,而不是插件声明。 jetty-maven-plugin 依赖于 Jetty 并且(即对 Jetty 的传递依赖)是 Jetty 类 被拉入 Maven 类路径的方式。
然而,该依赖项被声明为 'provided',基本上表示:"my code is dependent on this, but normally it will be provided by the deployment environment"。 Maven 将引入依赖关系,因为它需要它来构建您的源代码(这可能是 exec 插件起作用的原因),但不会将它包含在您的 jar 中,因为您明确告诉它这样做。
在我看来你的 pom 根本没有反映这种情况:
- 如果您的代码依赖于 Jetty 类(不是插件),请删除
jetty-maven-plugin dependency 并将 Jetty 本身声明为
明确的依赖。
- 不要将范围定义为 'provided' 除非
部署环境确实提供了 Jetty 类
(它没有,因为有错误)。
希望对您有所帮助!
我正在尝试配置 Maven 来构建一个 运行nable jar。 这是关于的项目包含几个依赖项以及嵌入式码头。
我想嵌入所有依赖项(有效)并拥有一个可执行 jar。 我可以从 eclipse 运行 项目正常,但是一旦我尝试 运行 罐子,我在 org.eclipse.jetty.servlet.ServletContextHandler.
上得到 ClassNotFoundExceptions但是,当我使用 maven-plugin 和 运行 exec:java 程序启动时没有问题。
我做错了什么?
这是我的副本 pom.xml
<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>comms</groupId>
<artifactId>comms-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Air</name>
<properties>
<jettyVersion>9.2.7.v20150116</jettyVersion>
<gsonVersion>2.3.1</gsonVersion>
<logjVersion>2.1</logjVersion>
<lombokVersion>1.16.2</lombokVersion>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jettyVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gsonVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${logjVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${logjVersion}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombokVersion}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals><goal>java</goal></goals>
</execution>
</executions>
<configuration>
<mainClass>comms.Loader</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<mainClass>comms.Loader</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
我注意到您将 jetty-maven-plugin 声明为 dependency,而不是插件声明。 jetty-maven-plugin 依赖于 Jetty 并且(即对 Jetty 的传递依赖)是 Jetty 类 被拉入 Maven 类路径的方式。
然而,该依赖项被声明为 'provided',基本上表示:"my code is dependent on this, but normally it will be provided by the deployment environment"。 Maven 将引入依赖关系,因为它需要它来构建您的源代码(这可能是 exec 插件起作用的原因),但不会将它包含在您的 jar 中,因为您明确告诉它这样做。
在我看来你的 pom 根本没有反映这种情况:
- 如果您的代码依赖于 Jetty 类(不是插件),请删除 jetty-maven-plugin dependency 并将 Jetty 本身声明为 明确的依赖。
- 不要将范围定义为 'provided' 除非 部署环境确实提供了 Jetty 类 (它没有,因为有错误)。
希望对您有所帮助!