无法在 Maven 构建中包含第三方 jar

Not able to include third party jar in Maven build

我构建了 awsblog-queueing-1.0.0.jar 大小为 15MB。

整个项目的构建,我也希望包含上述 JAR,但是只有 7 MB。

尝试 运行 生成的 JAR 给我:

 com/awsblog/queueing/appdata/ShipmentData: java.lang.NoClassDefFoundError
    java.lang.NoClassDefFoundError: com/awsblog/queueing/appdata/ShipmentData
        at com.test.App.handleRequest(App.java:34)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)

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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test</groupId>
    <artifactId>s3_trigger</artifactId>
    <packaging>jar</packaging>
    <name>s3_trigger</name>
    <version>1.0.0</version>

    <properties>
        <java.version>1.8</java.version>
        <sl4j.version>1.6.1</sl4j.version>
        <environment>local</environment>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <build.version>1.0.0</build.version>
        <maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss.SSS'Z'</maven.build.timestamp.format>
        <build-number>1.0.0</build-number>
        <maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss.SSS'Z'</maven.build.timestamp.format>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-lambda-java-core -->
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.2.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3 -->
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
            <version>1.11.271</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-log4j</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${sl4j.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-lambda-java-events -->
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-events</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>com.awsblog.queueing</groupId>
            <artifactId>awsblog-queueing</artifactId>
            <version>1.0.0</version>
            <scope>system</scope>
            <systemPath>C:/Users/abcd/Desktop/amazon-dynamodb-implementing-priority-queuing-master/target/awsblog-queueing-1.0.0.jar</systemPath>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <extensions>
            <extension>
                <groupId>org.springframework.build</groupId>
                <artifactId>aws-maven</artifactId>
                <version>5.0.0.RELEASE</version>
            </extension>
        </extensions>
    </build>
</project>

您需要使用 maven-install-plugin 将您的自定义 third-party JAR 包含到您的最终 JAR 中。

Apache Maven 有一个 guide,但专门针对您的情况:

  1. pom.xml 文件中删除 scope & systemPath XML 依赖项的 third-party 标签,因为它们不需要
  2. 运行 mvn install:install-file -Dfile=C:/Users/abcd/Desktop/amazon-dynamodb-implementing-priority-queuing-master/target/awsblog-queueing-1.0.0.jar -DgroupId=com.awsblog.queueing -DartifactId=awsblog-queueing -Dversion=1.0.0 在本地存储库中安装本地 JAR
  3. 运行 mvn clean package