简体   繁体   English

使用ant运行NetBeans项目

[英]Run NetBeans project just with ant

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? 是否有使用Java运行Netbeans项目的解决方法? What's the correct .properties file I have to modify? 什么是我必须修改的正确的.properties文件?

It is possible 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文件和引用属性来包含taskdef,这些文件和引用属性仅在/nbproject/private/private.properties文件中定义,这些文件通常在您第一次打开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您应该能够找到属性并派生需要设置的值(OS平台),然后:

  • create/set the property in the /nbproject/private.properties /nbproject/private.properties创建/设置该属性
  • add that property definition in the parent build.xml 在父build.xml添加该属性定义
  • pass in the commandline when invoking your ant target using -DPlatform=Foo 使用-DPlatform=Foo调用ant目标时传入命令行

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 

I've just successfully built NetBeans project with ant. 我刚刚用ant成功构建了NetBeans项目。 These were the things I had to do: 这些是我必须做的事情:

  • Copy <NetBeans-folder> /java2/ant to a "Netbeanless" machine <NetBeans-folder> / java2 / ant复制到“Netbeanless”计算机
  • Copy nbproject/project.properties to, say, ant.properties 将nbproject / project.properties复制到例如ant.properties
  • Replace every ${} expression in ant.properties with it's value 将ant.properties中的每个$ {}表达式替换为它的值
  • Add platform. 添加平台。 <platform-name> .home= <path to platform> <platform-name> .home = <path to platform>
  • Add libs.CopyLibs.classpath= <path to nb-ant> /extra/org-netbeans-modules-java-j2seproject-copylibtask.jar 添加libs.CopyLibs.classpath = <path to nb-ant> /extra/org-netbeans-modules-java-j2seproject-copylibtask.jar
  • Add other needed classpaths into javac.classpath (eg path to servlet-api.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). 我想要找到重用project.properties的方法,或者自动将其转换为“已解析”版本(步骤3)。 Build could then be automated. 然后可以自动构建构建。

I just went through this exercise with my NetBeans 7.0 project. 我刚刚使用NetBeans 7.0项目完成了这个练习。 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. 我能够做的是从我的Windows系统上的.netbeans \\ 7.0目录中复制我的build.properties文件,并为我的构建服务器创建一个server.properties文件。 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. 这并不是一件容易的事,因为每个开发人员的build.properties可能会有所不同,因此可以预期为服务器提供另一个文件。 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. 然后我将一个简单的server-build.xml文件放在一起,该文件引用了这个文件,并且只执行init,compile,dist和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. 我将这两个文件提交到我的项目目录顶层的CVS存储库,因为它们不与其他项目文件冲突,并在需要更新的情况下用作提醒。 Now I can build my project on my CI server with "ant -f server-build.xml" and everything just works. 现在,我可以使用“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. 我不得不将project.properties中的j2ee.platform.classpath复制到我的server.properties文件中,以确保根据需要解析对j2ee.server.home的引用。 I didn't expect to have to do this, but the classpath was wrong otherwise, causing the build to fail. 我没想到必须这样做,但是类路径错了,导致构建失败。

Thanks for the information on this question, as it helped guide me to this solution. 感谢您提供有关此问题的信息,因为它有助于我指导此解决方案。

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 link to export a ant built project from netbeans into a continuous integration server (or could be into any other IDE too). 但是这里有一个很好的链接,可以将netbeans中的ant构建项目导出到一个持续集成服务器(或者也可以进入任何其他IDE)。

Technique is going pretty well. 技术进展顺利。 Here is a summary: 以下是摘要:

  1. install netbeans on the CI server (there is an option to do it without gui, use -silent on the netbeans installer) 在CI服务器上安装netbeans(有一个没有gui的选项,在netbeans安装程序上使用-silent
  2. include nbproject in SVN 在SVN中包含nbproject
  3. add to ignore list the private folder 添加到忽略列表私人文件夹
  4. create your own private folder on CI server 在CI服务器上创建自己的私人文件夹
  5. make it point to a folder of your CI server (mimicking a private user folder in the account used for CI) 使其指向您的CI服务器的文件夹(模仿用于CI的帐户中的私人用户文件夹)
  6. 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安装。

And it should work. 它应该工作。

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. 我正在使用Netbeans 6.8,使用Netbeans创建的java项目可以从Netbeans自动生成的构建文件中运行,而cli上只有ant。 All the regular targets like ant compile , ant run , ant clean , etc "just work". 所有常规目标,如ant compileant runant clean等“正常工作”。 (I'm using Fedora 13 if that matters) (如果重要,我正在使用Fedora 13)

Just to add to Mads' answer... usually, you need to install and open Netbeans at least once on the target machine. 只是为了添加Mads的答案...通常,您需要在目标计算机上至少安装和打开一次Netbeans。 The ANT projects also rely on a few settings from the USERDIR/.netbeans/... directory. ANT项目还依赖于USERDIR / .netbeans / ...目录中的一些设置。 This may have changed with 6.5+. 这可能已经改变了6.5+。

This will get some of the base settings configured and define the classpath's to netbeans jars. 这将获得一些配置的基本设置,并将类路径定义为netbeans jar。 If your dependencies (ie 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中打开项目以来,您的依赖项(即库)或项目正在从不同的目录运行,则需要调整private.properties文件中的一些设置,如Mads所述。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM