gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:46:30 26 4
gpt4 key购买 nike

关注我的 question here我现在想到了这个问题。我正在使用 NetBeans 8。

我创建了一个 Maven 项目,我们称它为 MyLibMaven,(我使用了新建项目 -> Maven -> Java 应用程序)并将我的库(我们称它为 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. 将 Netbeans 项目作为库添加到现有 Netbeans 项目仅适用于“Netbeans 项目”(ant) 项目类型。
  2. 您需要将 Maven 项目生成的 Jar 及其所有依赖项添加到您的“Netbeans”项目中。因为“Netbeans (Ant) 项目”无法解决依赖关系,您必须手动解决这些依赖关系并将每个 Jar 文件添加为库条目。
  3. 您的 Maven 项目仅为您的项目生成 Jar,它不包括您的项目依赖项中的类/jar。

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

你可以...

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

我个人更喜欢第二种选择,但这是个人问题。作为我的 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>

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

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

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

你可以...

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

关于java - 无法添加此项目,因为它不使用 Ant 脚本生成 JAR 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24899985/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com