无法添加此项目,因为它不使用Ant脚本生成JAR文件

时间:2014-07-23 00:28:10

标签: java maven netbeans ant

跟随我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

1 个答案:

答案 0 :(得分:5)

您遇到的主要问题是......

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

    你可以......

    指示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复制到lib中的${project.build.directory}目录。这是个人的事情,但我不喜欢将Jar的内容合并到一个“主”Jar中,因为我倾向于使用与我在项目中用作查找的相同名称的资源...

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

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

    你可以......

    使您的第二个项目成为Maven项目,并将您的第一个项目作为依赖项包含在其中。然后,Maven将自动为您解析所有依赖项。这就是重点......