无法添加此项目,因为它不会使用 Ant 脚本生成 JAR 文件 [英] This project cannot be added because it does not produce a JAR file using an Ant script

查看:23
本文介绍了无法添加此项目,因为它不会使用 Ant 脚本生成 JAR 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关注我的这里的问题 我现在遇到了这个问题.我使用的是 NetBeans 8.

Following my question here I now came up to this problem. I'm using NetBeans 8.

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

I've created a Maven Project, let's call it MyLibMaven, (I used New Project -> Maven -> Java Aplication) and I moved my library, let's call it MyLib, into this Maven Project.

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

Now when I go to my normal Java Project (let's call it MyProject) and I try to add MyLibMaven as a library to MyProject I get this popup in Netbeans saying:

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

我在这里做错了什么?

这是我的 pom.xml

Here's my 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 作为依赖项.但是现在当我尝试运行我的项目时,我收到了这个失败的错误:

I moved my project to a maven project and added the MyLibMaven as a dependency. But now when I try to run my project I get this failed error:

--- 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

推荐答案

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

The main problems you are having is...

  1. 将 Netbeans 项目作为库添加到现有 Netbeans 项目仅适用于Netbeans 项目";(ant) 项目类型.
  2. 您需要将 Maven 项目生成的 Jar 及其所有依赖项添加到您的Netbeans"中.项目.因为一个Netbeans(Ant)项目"没有解决依赖的方法,你必须手动解决这些依赖,并将每个 Jar 文件添加为一个库条目.
  3. 您的 Maven 项目仅为您的项目生成 Jar,它不包括项目依赖项中的类/jar.

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

You have at least two basic options...

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

Instruct Maven to include all the dependencies as part of the build process. There are (at least) two ways to achieve this. You can include all the classes into a single Jar or have Maven copy all the dependencies to a specified location and update the class-path manifest entry.

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

I, personally, prefer the second option, but that's a personal thing. As part of my Maven build process, I include the following...

<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,因为我倾向于使用与我在项目中用作查找相同名称的资源...

Basically, the first plugin copies all the dependency Jars to the lib directory in the ${project.build.directory}. This is a personal thing, but I don't like merging the contents of the Jar into a single "master" Jar, as I tend to have resources with the same name which I uses as lookups in my project...

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

The second plugin includes the class-path element within the manifest file, this ensures that the dependent jars are included in the class loader lookup path at run time...

如果你想创建一个单"Jar,看一下使用Maven在jar中包含依赖

If you want to create a "single" Jar, take a look at Including dependencies in a jar with Maven

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

Make your second project a Maven project and include your first project as a dependency to it. Maven will then resolve all the dependencies automatically for you. That's kind of the point...

这篇关于无法添加此项目,因为它不会使用 Ant 脚本生成 JAR 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆