【问题标题】:Java Maven Clean and Build With DependenciesJava Maven 清理和构建依赖项
【发布时间】:2017-07-16 15:03:54
【问题描述】:

我在 java maven 和依赖项中仍然是新的。请问? 我使用 Java Maven 创建了一些关于 QR 码生成器的项目。 当我使用 qrgen-1.2.jar、core-2.0.jar 和 javase-2.0.jar 将我的项目运行到 Netbeans 中时。它可以生成我想要的任何二维码。

但是当我尝试构建和清理时,它无法在我的 Document/NetbeansProjects/QRcode/target/QRcode-1.0-SNAPSHOT.JR 中生成我的二维码

这里是我的 pom.xml

 <?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.mycompany</groupId>
<artifactId>QRcode</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
    <plugins>
        <plugin>                       
         <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version> 


          <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.mycompany.qrcode.QRcode</mainClass>
                    </manifest>
                </archive>
            </configuration>      
        </plugin>   
    </plugins>
</build>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>

</properties>

<dependencies>
        <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
        </dependency>

        <dependency>
        <groupId>net.glxn</groupId>
        <artifactId>qrgen</artifactId>
        <version>1.2</version>
         </dependency>                       

</dependencies>   

【问题讨论】:

    标签: java maven dependencies qr-code netbeans-8


    【解决方案1】:

    根据您的 POM,您不会将依赖项打包到生成的可执行 jar 中。当您在 IDE 之外运行程序时,这会导致您的程序失败。

    下面是一个如何使用 Maven Assembly 插件创建包含依赖项的可执行 jar 的示例:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.mycompany.qrcode.QRcode</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    mvn package 的输出现在将包含 target/QRcode-1.0-SNAPSHOT-jar-with-dependencies.jar,您可以看到它包含您的构建依赖项指定的类。

    这是插件的link to the documentation

    【讨论】:

    • 非常感谢先生,我的问题已经解决了。我很感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 2012-08-20
    • 2021-05-14
    相关资源
    最近更新 更多