Java 使用 ant 运行 NetBeans 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/912522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run NetBeans project just with ant
提问by OscarRyz
I have a sample code that was built with Netbeans.
我有一个使用 Netbeans 构建的示例代码。
It has a build.xml file so I downloaded ant and try to run it.
它有一个 build.xml 文件,所以我下载了 ant 并尝试运行它。
I've got this error message:
我收到此错误消息:
...... nbproject\build-impl.xml:76: Platform is not correctly set up
For what I can see, this is fixed by "simply" downloading Netbeans and running the sample from there, but... I don't want to install it to run a 10 files sample.
据我所知,这是通过“简单地”下载 Netbeans 并从那里运行示例来解决的,但是......我不想安装它来运行 10 个文件示例。
Is there a workaround to run Netbeans projects with Java? What's the correct .properties file I have to modify?
是否有使用 Java 运行 Netbeans 项目的解决方法?我必须修改的正确 .properties 文件是什么?
回答by Mads Hansen
It ispossible to run the NetBeans generated projects straight from Java/ANT, but you may need to manually set some of the properties and/or add paths to jar files.
它是可以运行直接从Java / ANT生成的项目在NetBeans,但您可能需要手动设置某些属性和/或添加路径jar文件。
Unfortunately, NetBeans tends to include taskdef's using their own JAR files and reference properties that are defined only in the /nbproject/private/private.properties
files, which usually get set when you first open the NetBeans project or modified as you edit the project in the IDE.
不幸的是,NetBeans 倾向于使用它们自己的 JAR 文件和仅在/nbproject/private/private.properties
文件中定义的引用属性来包含 taskdef,这些属性通常在您第一次打开 NetBeans 项目时设置或在您在 IDE 中编辑项目时进行修改。
If you inspect the build-impl.xml
you should be able to find the property and derive what value needs to be set(OS platform), then either:
如果您检查build-impl.xml
您应该能够找到该属性并推导出需要设置的值(操作系统平台),然后:
- create/set the property in the
/nbproject/private.properties
- add that property definition in the
parent
build.xml
- pass in the commandline when invoking your ant
target using
-DPlatform=Foo
- 创建/设置属性
/nbproject/private.properties
- 在父级中添加该属性定义
build.xml
- 使用调用您的 ant 目标时传入命令行
-DPlatform=Foo
Personally, I like the structure of the NetBeans generated ANT files and targets, but hate how much custom/proprietary stuff they jam in that makes it hard to run without NetBeans.
就我个人而言,我喜欢 NetBeans 生成的 ANT 文件和目标的结构,但讨厌它们塞入了多少自定义/专有的东西,这使得没有 NetBeans 很难运行。
For example:
例如:
ant -Dplatforms.JDK_1.7.home=/opt/jdk
回答by James Schek
Just to add to Mads'answer... usually, you need to install and open Netbeans at least once on the target machine. The ANT projects also rely on a few settings from the USERDIR/.netbeans/... directory. This may have changed with 6.5+.
只是为了添加Mads 的答案......通常,您需要在目标机器上至少安装并打开 Netbeans 一次。ANT 项目还依赖于 USERDIR/.netbeans/... 目录中的一些设置。这可能会随着 6.5+ 而改变。
This will get some of the base settings configured and define the classpath's to netbeans jars. If your dependencies (i.e. libraries) or project is being run from a different directory since the last time you opened the project in Netbeans, you will need to tweak a few settings in the private.properties file as Mads' described.
这将配置一些基本设置并定义类路径到 netbeans jar。如果自上次在 Netbeans 中打开项目以来,您的依赖项(即库)或项目是从不同目录运行的,您将需要按照 Mads 的描述调整 private.properties 文件中的一些设置。
回答by Slartibartfast
I've just successfully built NetBeans project with ant. These were the things I had to do:
我刚刚用 ant 成功构建了 NetBeans 项目。这些是我必须做的事情:
- Copy
<NetBeans-folder>
/java2/ant to a "Netbeanless" machine - Copy nbproject/project.properties to, say, ant.properties
- Replace every ${} expression in ant.properties with it's value
- Add platform.
<platform-name>
.home=<path to platform>
- Add libs.CopyLibs.classpath=
<path to nb-ant>
/extra/org-netbeans-modules-java-j2seproject-copylibtask.jar - Add other needed classpaths into javac.classpath (e.g. path to servlet-api.jar)
ant -propertyfile ant.properties
- 将
<NetBeans-folder>
/java2/ant复制到“Netbeanless”机器 - 将 nbproject/project.properties 复制到 ant.properties
- 用它的值替换 ant.properties 中的每个 ${} 表达式
- 添加平台。
<platform-name>
.home=<path to platform>
- 添加 libs.CopyLibs.classpath=
<path to nb-ant>
/extra/org-netbeans-modules-java-j2seproject-copylibtask.jar - 将其他需要的类路径添加到 javac.classpath(例如 servlet-api.jar 的路径)
ant -propertyfile ant.properties
It works, but doesn't make me happy. I would either like to find the way to reuse project.properties, or to automatically translate it to a "resolved" version (step 3). Build could then be automated.
它有效,但不会让我开心。我要么想找到重用 project.properties 的方法,要么将其自动转换为“已解决”版本(第 3 步)。然后可以自动化构建。
回答by Leif Gruenwoldt
I'm using Netbeans 6.8 and java projects that were created with Netbeans can be run from the Netbeans auto generated build files with just ant on the cli. All the regular targets like ant compile
,ant run
,ant clean
, etc "just work". (I'm using Fedora 13 if that matters)
我正在使用 Netbeans 6.8,并且使用 Netbeans 创建的 java 项目可以从 Netbeans 自动生成的构建文件中运行,而 cli 上只有 ant。所有的常规指标喜欢ant compile
,ant run
,ant clean
,等“只是工作。” (如果重要的话,我正在使用 Fedora 13)
回答by Steve Ferguson
I just went through this exercise with my NetBeans 7.0 project. What I was able to do was copy my build.properties file from my .netbeans\7.0 directory on my Windows system and make a server.properties file for my build server. This isn't much of a stretch, since every developer's build.properties may vary, so having another file for the server is to be expected. I then put together a simple server-build.xml file that references this file and does only the basics of init, compile, dist, and clean. I committed these two files to my CVS repository at the top level of my project directory, since they don't conflict with other project files and serve as a reminder in case something needs to be updated. Now I can build my project on my CI server with "ant -f server-build.xml" and everything just works.
我刚刚通过我的 NetBeans 7.0 项目完成了这个练习。我能够做的是从我的 Windows 系统上的 .netbeans\7.0 目录复制我的 build.properties 文件,并为我的构建服务器创建一个 server.properties 文件。这并不复杂,因为每个开发人员的 build.properties 可能会有所不同,因此应该为服务器提供另一个文件。然后我将一个简单的 server-build.xml 文件放在一起,该文件引用了这个文件,并且只完成了 init、compile、dist 和 clean 的基本操作。我将这两个文件提交到我项目目录顶层的 CVS 存储库中,因为它们不与其他项目文件冲突,并在需要更新某些内容时用作提醒。现在我可以使用“ant -f server-build.xml”在我的 CI 服务器上构建我的项目,一切正常。
My whole init section looks like this, giving my server paths priority, but including the necessary information from the NetBeans project properties.
我的整个 init 部分看起来像这样,给予我的服务器路径优先级,但包括来自 NetBeans 项目属性的必要信息。
<target name="init">
<property file="server.properties"/>
<property file="nbproject/project.properties"/>
</target>
I also had to do something similar when defining the ant tasks for my nested projects:
在为我的嵌套项目定义 ant 任务时,我也必须做类似的事情:
<target name="compile">
<ant antfile="${project.MyProj-common}/build.xml" inheritall="false" target="jar">
<property location="${build.dir}" name="dist.ear.dir"/>
<property file="server.properties"/>
<property file="${project.MyProj-common}/nbproject/project.properties"/>
</ant>
...
</target>
I had to copy the j2ee.platform.classpath from project.properties to my server.properties file to ensure the references to j2ee.server.home resolved as I needed. I didn't expect to have to do this, but the classpath was wrong otherwise, causing the build to fail.
我必须将 j2ee.platform.classpath 从 project.properties 复制到我的 server.properties 文件,以确保根据需要解析对 j2ee.server.home 的引用。我没想到必须这样做,但否则类路径是错误的,导致构建失败。
Thanks for the information on this question, as it helped guide me to this solution.
感谢您提供有关此问题的信息,因为它帮助我找到了此解决方案。
回答by Snicolas
I just faced the same problem. I hope I could get rid of netbeans to go to eclipse and maven, just for that.
我刚刚面临同样的问题。我希望我可以摆脱 netbeans 去 eclipse 和 maven,只是为了那个。
But here is a good linkto export a ant built project from netbeans into a continuous integration server (or could be into any other IDE too).
但这里有一个很好的链接,可以将 ant 构建的项目从 netbeans 导出到持续集成服务器(或者也可以导出到任何其他 IDE)。
Technique is going pretty well. Here is a summary:
技术还不错。这是一个总结:
- install netbeans on the CI server (there is an option to do it without gui, use
-silent
on the netbeans installer) - include
nbproject
in SVN - add to ignore list the private folder
- create your own private folder on CI server
- make it point to a folder of your CI server (mimicking a private user folder in the account used for CI)
- copy a real folder from a user to this folder and change every path (replace strings) to point to your netbeans install on your CI server.
- 在 CI 服务器上安装 netbeans(有一个选项可以在没有 gui 的情况下进行,
-silent
在 netbeans 安装程序上使用) - 包含
nbproject
在 SVN 中 - 添加忽略列表私有文件夹
- 在 CI 服务器上创建您自己的私人文件夹
- 使其指向 CI 服务器的文件夹(模仿用于 CI 的帐户中的私人用户文件夹)
- 将用户的真实文件夹复制到此文件夹并更改每个路径(替换字符串)以指向 CI 服务器上的 netbeans 安装。
And it should work.
它应该工作。
回答by albfan
You can use this repo just for that https://github.com/albfan/ant-netbeans
您可以仅将这个 repo 用于https://github.com/albfan/ant-netbeans
It overcomes all the oddities of netbeans wrapped ant config so you just need:
它克服了 netbeans 包装的 ant 配置的所有奇怪之处,因此您只需要:
To compile:
编译:
$ ant.sh compile
To run:
跑步:
$ ant.sh run
To whatever (autocompletion):
随便(自动完成):
$ ant.sh <tab><tab>