如何让 Netbeans 自动将第 3 方 jar 从包含的类库复制到我的项目的 dist/lib 目录?

发布于 2025-01-03 07:27:39 字数 844 浏览 1 评论 0原文

我在 NetBeans 7.1 中有一个 Java 应用程序(我们称之为 myApp),它依赖于一个 Java 类库项目(实际上不止一个),我在其中保留了一些在项目之间共享的实用程序类。

这个类库项目(我们称之为 myLib)依赖于[许多]第三方库(例如 apache-commons-younameit.jar)。

我将 myLib 作为库包含在内,NetBeans 的 Ant 脚本将 myLib.jar 提取到 myApp 项目的 dist/lib 目录中;但是,myLib 所依赖的 jar 并未与 myLib 一起拉入,因此我的项目由于缺少 apache-commons-youtnameit.jar 而出现运行时异常。

我希望将 myLib.jar 和 apache-commons-younameit.jar 自动拉入 myApp 的 dist/lib 文件夹中,这样我就不必手动检查所有库的依赖项和将它们添加到我的主项目中。

  • 是否有一种明显的方法可以通过我缺少的 NetBeans 对话框来完成此操作?
  • 或者是否有必要手动调整我的 build.xml?如果是这样,您能否发布一个示例?(我对 Ant 不太了解)

我想避免如下:

  1. 我需要添加一个新的实用程序库,从我的 Java 类库项目中挑选。
  2. 我在该库的库中查找使用了哪些罐子并将其写下来。
  3. 我返回到我的主项目并添加那些第三方库(仅添加那些我尚未包含在我的主项目本身中直接使用的库)。

预先感谢您提供任何帮助,或为我指明正确的方向。

I have a Java Application in NetBeans 7.1 (let's call it myApp) that depends on a Java Class-Library Project [more than one, actually] where I keep some utility-classes I share among projects.

This Class-Library Project (let's call it myLib) depends on [many] third party libraries (e.g. apache-commons-younameit.jar).

I included myLib as a library, and NetBeans' Ant script pulls myLib.jar in the dist/lib directory of my myApp project; however, the jars upon which myLib depends are not pulled in together with myLib, so my project gets runtime exceptions due to the missing apache-commons-youtnameit.jar.

I would like to have both myLib.jar and apache-commons-younameit.jar automatically pulled into myApp's dist/lib folder, so that I don't have to manually check all of my libraries' dependencies and add them to my main project.

  • Is there an obvious way to accomplish this through NetBeans dialogs that I'm missing?
  • or is it necessary to manually tweak my build.xml? If so, could you kindly post an example? (I'm not that well up on Ant)

What I'd like to avoid is the following:

  1. I need to add a new utility library, picking from my Java Class-Library Projects.
  2. I lookup into that library's libraries what jars are used and write them down.
  3. I go back to my main project and add those 3rd party libraries (only the ones that I haven't included already for direct use from within my main project itself).

Thank you in advance for any help, or for pointing me in the right direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

玩套路吗 2025-01-10 07:27:39

正如您在评论中所说,我找到了一种复制“依赖库的依赖库”的方法。希望这有帮助。

将以下代码添加到 NetBean 项目的 build.xml 文件中的 行之后。您需要在下面的 标记中填写正确的相对路径。

<!--Copy over any "dependant libraries' dependant libraries" -->
<target name="-post-jar">
    <echo>Copying over extra jars into PROJECTNAME</echo>
    <copy todir="./dist/lib">
        <!-- put in order from least important to most important to prevent file overwrites -->
        <fileset dir="path-to-other-nb-project/dist/lib"></fileset>
        <fileset dir="path-to-second-nb-project/dist/lib"></fileset>
    </copy>
</target>

不是最干净的代码,但它为我解决了问题。当您向主项目添加额外的依赖项目时,您需要手动更新此项目。

I found a way to copy "dependant libraries' dependant libraries" as you said in one of the comments. Hopefully this helps.

Add the following code to your NetBean project's build.xml file, after the <import file="nbproject/build-impl.xml"/> line. You will need to fill out a proper relative path in the <fileset> tags below.

<!--Copy over any "dependant libraries' dependant libraries" -->
<target name="-post-jar">
    <echo>Copying over extra jars into PROJECTNAME</echo>
    <copy todir="./dist/lib">
        <!-- put in order from least important to most important to prevent file overwrites -->
        <fileset dir="path-to-other-nb-project/dist/lib"></fileset>
        <fileset dir="path-to-second-nb-project/dist/lib"></fileset>
    </copy>
</target>

Not the cleanest code, but it solved the problem for me. You'll need to manually update this as you add extra dependant projects to your main project.

绝影如岚 2025-01-10 07:27:39

这就是 Netbeans 7.3 中对我有用的方法:

右键单击您的项目并选择“属性”,然后选择右侧的“库”。

然后在“库文件夹”下单击“浏览...”按钮:
Propertiesdialog

然后选择您的 lib 文件夹,除非 Netbeans 已自动选择它:
添加库文件夹对话框

然后单击“下一步”,根据需要完成,当您返回“属性”对话框时,您应该看到:
属性对话框最终设置

之后,Netbeans 应该将 lib 文件夹复制到 dest 中,最重要的是,将引用所有库在主 JAR 的 MANIFEST.MF 文件中。

作为测试,查看 lib 文件夹中是否有新的 CopyLibs 文件夹。 CopyLibs 内部应该有一个 org-netbeans-modules-java-j2seproject-copylibstask.jar 文件。 Netbeans 添加了这些,我认为这就是推动 lib 文件夹复制到 dist 的原因。

This is what worked for me in Netbeans 7.3:

Right-click on your project and select Properties, then select Libraries on the right side.

Then under "Libraries Folder" click the "Browse..." button:
Properties dialog

Then choose your lib folder, unless it is already chosen automatically by Netbeans:
Add Libraries Folder dialog

Then click Next, Finish as needed and when you come back to the Properties dialog, you should have:
Properties dialog final Setup

After that, Netbeans should both copy the lib folder into dest and, most importantly, will reference all your libraries in the MANIFEST.MF file in your main JAR.

As a test, look if there is a new CopyLibs folder in the lib folder. Inside CopyLibs there should be a org-netbeans-modules-java-j2seproject-copylibstask.jar file. Netbeans added these and this is, I presume, what drives the copying of the lib folder into dist.

指尖上的星空 2025-01-10 07:27:39

我遇到了同样的问题,我编写了一个 ant 任务来自动将所有包含的类库项目中的依赖库复制到我的主项目中。您不需要包含任何第三方库即可运行。

必须设置类库项目选项中的“复制依赖库”复选框才能使其正常工作。然后 Netbeans 将所有依赖库复制到类库项目的 dist/lib 文件夹中,类似于 Gregory 的解决方案。此方法的优点是,当您在主项目中包含更多类库时,无需每次都调整 build.xml

它是如何工作的

我的 ant 任务首先在主项目的属性中搜索所有包含项目的路径,然后打开其 project.properties 文件,读取类库项目的 dist 文件夹并将所有库复制到dist-folder 的 lib 目录复制到 main-project 的 lib 文件夹。

还有一个测试是否启用“复制依赖库”。

代码

项目父文件夹中的文件 build-import-libraries.xml

<?xml version="1.0" encoding="UTF-8"?>
 <!--
      Custom import of libraries.
      This is not done in Netbeans, because the dist folder is created at runtime.

      To work correctly, disable the compile on save feature for your project.

      Author: Christophe Weis
 -->
<project name="IncludeLibraries" default="default" basedir=".">
    <description>Includes dependent libraries in projects using class-library projects.</description>
    <dirname property="IncludeLibraries.basedir" file="${ant.file.IncludeLibraries}"/>
    <target name="copy-files">
        <!-- Set variable to the folder where to copy the files to -->
        <condition property="libdestdir" value="${build.web.dir}/WEB-INF/lib" else="${dist.dir}/lib">
            <isset property="build.web.dir"/>
        </condition>
        <echo message="Custom build step in file '${ant.file}': copying dependent libraries from included Netbeans projects to '${libdestdir}'" level="info"/>
        <echo message="Please make sure that 'Copy Dependent Libraries' is enabled in Netbeans project options for each included project" level="info"/>
        <copy-dependent-libs>
            <propertyset id="properties-starting-with-project">
                <propertyref prefix="project."/>
            </propertyset>
        </copy-dependent-libs>
    </target> <!-- End custom import -->

    <scriptdef name="copy-dependent-libs" language="javascript">
        <element name="propertyset" type="propertyset"/>
        <![CDATA[
            // Ant uses the Rhino JavaScript implementation

            propertySets = elements.get("propertyset");
            // loop all nested property sets
            for (i = 0; i < propertySets.size(); ++i) {
                propertySet = propertySets.get(i);
                properties = propertySet.getProperties();
                for (var iterator = properties.entrySet().iterator(); iterator.hasNext();) {
                    var entry = iterator.next();
                    var key = entry.getKey();
                    if ("project.licensePath".equals(key)) {
                        continue;
                    }
                    var value = entry.getValue();

                    // read the referenced project's property file
                    var file = new java.io.File(project.getBaseDir(), value + "/nbproject/project.properties");
                    var inputStream = new java.io.FileInputStream(file);
                    var projectProperties = new java.util.Properties();
                    projectProperties.load(inputStream);
                    inputStream.close();
                    var distFolder = projectProperties.getProperty("dist.dir");

                    // check if 'Copy Dependent Libraries' is enabled
                    var doNotCopyDependentLibraries = projectProperties.getProperty("mkdist.disabled");
                    doNotCopyDependentLibraries = java.lang.Boolean.parseBoolean(doNotCopyDependentLibraries);
                    if (doNotCopyDependentLibraries) {
                        self.fail("Please enable 'Copy Dependent Libraries' in project properties for project " + projectProperties.getProperty("application.title", value));
                    }

                    t = project.createTask("copy-files-from-libfolder");
                    // set the dist folder's lib directory
                    t.setDynamicAttribute("fromdir", value + "/" + distFolder + "/lib");
                    t.perform();
                }
            }
        ]]>
    </scriptdef>

    <macrodef name="copy-files-from-libfolder">
        <attribute name="fromdir" />
        <sequential>
            <echo message="Copying libraries from directory '@{fromdir}' to '${libdestdir}'" level="verbose" />
            <copy todir="${libdestdir}">
                <fileset dir="@{fromdir}">
                    <!-- Enable this if you wish
                    <exclude name="servlet-api.jar"/>
                    -->
                </fileset>
            </copy>
        </sequential>
    </macrodef>

</project>

将此行包含在 build.xml 中,位于

<!-- 
     Custom import of libraries.
-->
<import file="../build-import-libraries.xml" as="include-libraries" />
<target name="-post-compile" depends="include-libraries.copy-files" />

这适用于 Java SE 应用程序和 Java Web 应用程序。我只在 Netbeans 版本 8.0.2 中对此进行了测试。我希望这在未来的版本中继续有效。

I had the same problem and I wrote an ant task to automatically copy the dependent libraries from all included class-library projects into my main-project(s). You don't need to include any third party libraries for this to run.

The Copy Dependent Libraries checkbox in the class-library project options must be set for this to work. Then Netbeans copies all dependent libraries into the class-library project's dist/lib folder, similar to Gregory's solution. The advantage of this method is, when you include more class-libraries to your main-project, you don't need to adapt your build.xml each time.

How it works

My ant task first searches the main-project's properties for the paths of all included projects, then opens their project.properties file, reads the class-library project's dist-folder and copies all libraries in the dist-folder's lib directory to the main-project's lib folder.

There is also a test if Copy Dependent Libraries is enabled.

The code

The file build-import-libraries.xml in the project's parent folder :

<?xml version="1.0" encoding="UTF-8"?>
 <!--
      Custom import of libraries.
      This is not done in Netbeans, because the dist folder is created at runtime.

      To work correctly, disable the compile on save feature for your project.

      Author: Christophe Weis
 -->
<project name="IncludeLibraries" default="default" basedir=".">
    <description>Includes dependent libraries in projects using class-library projects.</description>
    <dirname property="IncludeLibraries.basedir" file="${ant.file.IncludeLibraries}"/>
    <target name="copy-files">
        <!-- Set variable to the folder where to copy the files to -->
        <condition property="libdestdir" value="${build.web.dir}/WEB-INF/lib" else="${dist.dir}/lib">
            <isset property="build.web.dir"/>
        </condition>
        <echo message="Custom build step in file '${ant.file}': copying dependent libraries from included Netbeans projects to '${libdestdir}'" level="info"/>
        <echo message="Please make sure that 'Copy Dependent Libraries' is enabled in Netbeans project options for each included project" level="info"/>
        <copy-dependent-libs>
            <propertyset id="properties-starting-with-project">
                <propertyref prefix="project."/>
            </propertyset>
        </copy-dependent-libs>
    </target> <!-- End custom import -->

    <scriptdef name="copy-dependent-libs" language="javascript">
        <element name="propertyset" type="propertyset"/>
        <![CDATA[
            // Ant uses the Rhino JavaScript implementation

            propertySets = elements.get("propertyset");
            // loop all nested property sets
            for (i = 0; i < propertySets.size(); ++i) {
                propertySet = propertySets.get(i);
                properties = propertySet.getProperties();
                for (var iterator = properties.entrySet().iterator(); iterator.hasNext();) {
                    var entry = iterator.next();
                    var key = entry.getKey();
                    if ("project.licensePath".equals(key)) {
                        continue;
                    }
                    var value = entry.getValue();

                    // read the referenced project's property file
                    var file = new java.io.File(project.getBaseDir(), value + "/nbproject/project.properties");
                    var inputStream = new java.io.FileInputStream(file);
                    var projectProperties = new java.util.Properties();
                    projectProperties.load(inputStream);
                    inputStream.close();
                    var distFolder = projectProperties.getProperty("dist.dir");

                    // check if 'Copy Dependent Libraries' is enabled
                    var doNotCopyDependentLibraries = projectProperties.getProperty("mkdist.disabled");
                    doNotCopyDependentLibraries = java.lang.Boolean.parseBoolean(doNotCopyDependentLibraries);
                    if (doNotCopyDependentLibraries) {
                        self.fail("Please enable 'Copy Dependent Libraries' in project properties for project " + projectProperties.getProperty("application.title", value));
                    }

                    t = project.createTask("copy-files-from-libfolder");
                    // set the dist folder's lib directory
                    t.setDynamicAttribute("fromdir", value + "/" + distFolder + "/lib");
                    t.perform();
                }
            }
        ]]>
    </scriptdef>

    <macrodef name="copy-files-from-libfolder">
        <attribute name="fromdir" />
        <sequential>
            <echo message="Copying libraries from directory '@{fromdir}' to '${libdestdir}'" level="verbose" />
            <copy todir="${libdestdir}">
                <fileset dir="@{fromdir}">
                    <!-- Enable this if you wish
                    <exclude name="servlet-api.jar"/>
                    -->
                </fileset>
            </copy>
        </sequential>
    </macrodef>

</project>

Include this line in your build.xml, after <import file="nbproject/build-impl.xml"/> (you may need to adapt the path to build-import-libraries.xml in the import task) :

<!-- 
     Custom import of libraries.
-->
<import file="../build-import-libraries.xml" as="include-libraries" />
<target name="-post-compile" depends="include-libraries.copy-files" />

This works for Java SE Applications and Java Web Applications. I have only tested this in Netbeans version 8.0.2. I hope this will keep working in future versions.

栀梦 2025-01-10 07:27:39

Lee 的答案适用于 NetBeans 7.2 中的 Java 应用程序项目。如果要在 NetBeans 中使用 Maven,则必须创建一个 Maven Java 应用程序项目。修改 pom.xml 文件以包含 maven组装插件

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>your.app.MainClass</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Lee's answer works with a Java Application project in NetBeans 7.2. If you want to use Maven in NetBeans, you'll have to create a Maven Java Application project. Modify the pom.xml file to include the maven assembly plugin:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>your.app.MainClass</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
枫林﹌晚霞¤ 2025-01-10 07:27:39

假设您已将它们全部添加到项目类路径中(在项目属性中的 Libraries 下)。您可以进入 Build >;打包并勾选复制依赖库

我正在使用 Netbeans 7.2。

Assuming that you have added them all to your project classpath(under Libraries in the project properties). You can go into Build > Packaging and tick Copy Dependant Libraries.

I am using Netbeans 7.2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文