我可以打包一个我写过的JAVA项目。一个使用Gson
库来实现JSON功能。我对JAVA很新,所以我可能犯了一个愚蠢的错误,但这就是我所假设的:
在我的源代码中:
import com.google.gson.Gson;
然后像这样使用这个导入:
Gson gson = new Gson();
String json = gson.toJson(result);
在我的Maven pom.xml
中,我在依赖项部分中包含以下内容:
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
正如我所说,它确实打包到一个没有错误的JAR文件(使用Maven进行打包),但是我的JAR文件正在AWS上用作无服务器功能,所以我相信我需要做的是将Gson依赖包括在内我的JAR文件(可能是错的)。这似乎得到了我在AWS上遇到的错误的支持:
做了一些谷歌搜索,看起来好像Maven的“阴影”插件可能是票。我把它添加到我的pom.xml
中就像这样:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<artifactSet>
<includes>
<include>com/google/gson/**</include>
</includes>
</artifactSet>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
但是查看生成的JAR文件,我发现没有区别。我需要做些什么才能让它发挥作用?
完整的POM文件可以在这里找到:pom.xml
我不熟悉其他人引用的shade
插件。对我来说,你需要一个可执行jar的工件:一个带有依赖关系的jar。
以下是我使用Maven的方法:
<?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.company.groupId</groupId>
<artifactId>artifact-id</artifactId>
<version>1.0.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.company.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-javadoc</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-source</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
你要注意的是构建插件,maven-assembly-plugin
。这告诉Maven如何组装/打包构建的结果。在其配置中,您定义包含runnable应用程序的主类,该应用程序通常是您的public static void main(String[] args)
声明所在的位置。您还定义了一个descriptor-ref,它是一个将附加到jar名称的String。所以,你最终使用artifactId-1.0.0-jar-with-dependencies.jar
,以我的POM为例。
为了进一步解释发生了什么,没有我推荐的更改,你的POM告诉Maven只是构建你的代码。作为其中的一部分,您声明了依赖关系,您现在有两个依赖关系:aws-lambda-java-core
和gson
。如果未提供范围,则默认为compile
范围。这告诉Maven在编译程序时获取该依赖项,以便程序可以使用该依赖项的代码。但是,在打包程序的构建工件时,Maven默认情况下不会在最终的jar中包含这些依赖项;它期望当你运行jar时,你将在类路径上拥有这些依赖项。
通过添加程序集构建插件,您可以将这些指令更改为Maven。有了这个插件,你告诉Maven,当它构建程序时,它需要以这样的方式组装它,即所有声明的依赖项都包含在程序中(读取:汇编),并在程序包的阶段执行。建立;您将在构建工件的lib
文件夹中看到这些依赖项。然后,就像我之前提到的那样,descriptorRef是描述性信息,将附加到构建工件的名称上。
顺便说一下,与你的问题并不真正相关,我建议你查看FasterXML的JSON处理和操作。功能更强大,更容易,并得到广泛支持和使用,这意味着它背后有一个很好的社区。
不幸的是我没有足够的声誉来评论,所以我必须“回答”。但是你还在你的pom.xml中使用maven-jar-plugin
吗?这可能会导致功能上出现一些重叠。
如果是这样,你可能想要删除它,并考虑将这些添加到configuration
的maven-shade-plugin
部分,以确保你已经宣布要运行的mainClass
。 <createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>main.java.***.ClassName</mainClass>
</transformer>
</transformers>
如果您具有将从运行时容器提供的依赖项,则应设置为这些依赖项提供的范围。
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.0.0</version>
<scope>provided</scope>
</dependency>
并从插件配置中删除<artifactSet>
部分,执行mvn package
然后将创建具有所需依赖关系的jar。