【问题标题】:This project cannot be added because it does not produce a JAR file using an Ant script无法添加此项目,因为它不使用 Ant 脚本生成 JAR 文件
【发布时间】:2014-09-14 00:25:31
【问题描述】:

按照我的question here,我现在遇到了这个问题。 我正在使用 NetBeans 8。

我创建了一个 Maven 项目,我们称之为 MyLibMaven,(我使用了 New Project -> Maven -> Java Aplication)并将我的库(我们称之为 MyLib)移动到了这个 Maven 项目中。

现在,当我转到我的普通 Java 项目(我们称之为 MyProject)并尝试将 MyLibMaven 作为库添加到 MyProject 时,我在 Netbeans 中看到这个弹出窗口:

This project cannot be added because it does not produce a JAR file using an Ant script

我在这里做错了什么?

这是我的 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.dwnz.my.lib</groupId>
    <artifactId>MyLib</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    <name>MyLib</name>

    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>17.0</version>
        </dependency>
    </dependencies>
</project>

////////////////////// 编辑/////////////// ////////////

我将我的项目移动到一个 maven 项目并添加了 MyLibMaven 作为依赖项。但是现在当我尝试运行我的项目时,我得到了这个失败的错误:

--- exec-maven-plugin:1.2.1:exec (default-cli) @ TeseMaven ---
Could not load the icon: /view/images/enable-server-icon.png
Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
    at view.toolbar.ToolBar.createIcon(ToolBar.java:171)
    at view.toolbar.ToolBar.<init>(ToolBar.java:53)
    at view.MainFrame.<init>(MainFrame.java:98)
    at view.MainFrame.newInstance(MainFrame.java:586)
    at view.Main.main(Main.java:98)
------------------------------------------------------------------------

------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 2.382s
Finished at: Wed Jul 23 02:13:22 BST 2014
Final Memory: 5M/126M
------------------------------------------------------------------------
Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project MyProjectMaven: Command execution failed. Process exited with an error: 1 (Exit value: 1) -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

【问题讨论】:

  • 只有当您尝试添加的项目是实际的“Netbeans”“项目”类型时,才能将“Netbeans”项目添加到现有“Netbeans”项目的库中。 Netbeans 不以这种方式提供对其他项目类型的支持。您将需要构建和部署您的 Maven 项目,然后将生成的 .jar 文件(及其依赖项)添加到您的“Netbeans”项目中。一个更好的解决方案是让你的第二个项目也成为一个 Maven 项目,然后简单地将你的第一个项目添加为它的依赖项......这就是 Maven 的重点
  • @MadProgrammer 你能解释一下或给我一个关于如何构建和部署我的 Maven 项目的教程的链接吗?我确实在 MyLibMaven 上尝试了“清理和构建”,我在...NetBeansProjects\MyLibMaven\target 看到了一个 .jar 文件。如果我将该 .jar 文件添加到 MyProject 中,我会从我之前的 SO 问题中得到异常(链接在这个 SO 问题中)。我现在只需要解决这个问题以进行快速部署,明天我将按照您在评论第二部分中的建议将 MyProject 移动到 Maven 项目。
  • 您可以右键单击 Maven 项目并选择“自定义->目标”并键入部署。这会将 Jar 部署到您的本地存储库,具体取决于它的所有配置方式
  • 您可能遇到的另一个问题是您的 Maven 项目没有构建依赖项。我倾向于使用copy-dependencies 插件,它将所有依赖项复制到指定位置,我还包括&lt;configuration&gt;&lt;archive&gt;&lt;manifest&gt; 插件,它允许我将class-path 元素添加到清单中,其中包括所有依赖项

标签: java maven netbeans ant


【解决方案1】:

你遇到的主要问题是……

  1. 将 Netbeans 项目作为库添加到现有 Netbeans 项目仅适用于“Netbeans 项目”(ant) 项目类型。
  2. 您需要将 Maven 项目生成的 Jar 及其所有依赖项添加到您的“Netbeans”项目中。由于“Netbeans (Ant) 项目”无法解析依赖项,因此您必须手动解析这些依赖项并将每个 Jar 文件添加为库条目。
  3. 您的 Maven 项目只是为您的项目生成 Jar,它不包括您的项目依赖项中的类/jar。

您至少有两个基本选择...

你可以...

指示 Maven 在构建过程中包含所有依赖项。有(至少)两种方法可以实现这一点。您可以将所有类包含到单个 Jar 中,或者让 Maven 将所有依赖项复制到指定位置并更新 class-path 清单条目。

我个人更喜欢第二种选择,但这是个人的事情。作为我的 Maven 构建过程的一部分,我包括以下内容...

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <!--mainClass>com.acme.MainClass</mainClass-->
                    </manifest>
                </archive>
            </configuration>
        </plugin>        

基本上,第一个插件将所有依赖项 Jars 复制到 ${project.build.directory} 中的 lib 目录中。这是个人的事情,但我不喜欢将 Jar 的内容合并到一个“主”Jar 中,因为我倾向于拥有同名的资源,在我的项目中用作查找...

第二个插件在清单文件中包含class-path 元素,这确保了依赖的 jar 在运行时包含在类加载器查找路径中...

如果你想创建一个“单个”Jar,请查看Including dependencies in a jar with Maven

你可以...

将您的第二个项目设为 Maven 项目,并将您的第一个项目作为对它的依赖项。然后,Maven 将自动为您解析所有依赖项。这就是重点......

【讨论】:

  • 你是否构建并部署了MyLibMaven
  • 在“构建失败”消息之前是否有任何编译器错误
  • 哦,我的错,我没有粘贴所有的消息。我已经编辑了我的问题。它有一个空指针异常,但该文件确实存在并且位于正确的文件夹中
  • /view/images/enable-server-icon.png 是否驻留在src/main/resources 目录中?
  • 不,我没有任何资源文件夹。 enable-server-icon.png 位于 src\main\java\view\images
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-07
  • 2019-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-26
相关资源
最近更新 更多