java Netbeans 和外部配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/301283/
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
Netbeans and external configuration files
提问by Joakim Karlsson
I am developing a Java desktop application and would like to have an external configuration.xml.
I am developing the application using Netbeans and tried to add the configuration.xml file in the dist directory so that it resides in the application work folder. But when Netbeans executes its clean operation it deletes the dist directory,
Where should I put this configuration.xml file so that it will not be deleted and will exist in the application start-up directory.
我正在开发一个 Java 桌面应用程序,并希望有一个外部 configuration.xml。
我正在使用 Netbeans 开发应用程序并尝试在 dist 目录中添加 configuration.xml 文件,以便它驻留在应用程序工作文件夹中。但是当Netbeans 执行它的clean 操作时它会删除dist 目录,
我应该把这个configuration.xml 文件放在哪里,这样它就不会被删除,并且会存在于应用程序启动目录中。
回答by Michel
You can add this to your build.xml :
您可以将其添加到 build.xml :
<target name="-post-jar">
<copy todir="${dist.jar.dir}">
<fileset dir="resources" includes="**"/>
</copy>
</target>
You can now put your configuration.xml file in the folder 'resources' (that you need to create) in your project and all files in it will be copied to the dist folder during the build process.
您现在可以将 configuration.xml 文件放在项目中的文件夹“resources”(您需要创建)中,并且在构建过程中,其中的所有文件都将复制到 dist 文件夹中。
回答by Robert Casey
I was able to get this to work, but I couldn't get -post-jar to trigger without explicitly entering it as a dependency in the main build config. This is in Netbeans 7.0.1 for a Rich Client project.
我能够让它工作,但我无法在没有明确输入它作为主构建配置中的依赖项的情况下触发 -post-jar。这是在 Netbeans 7.0.1 中的 Rich Client 项目。
Instead, in build.xml for the Netbeans module where I want to have external resource files (mainly .txt files that the user could potentially edit later), I entered the following:
相反,在 Netbeans 模块的 build.xml 中,我希望在其中拥有外部资源文件(主要是用户以后可能会编辑的 .txt 文件),我输入了以下内容:
<target name="netbeans-extra">
<echo>Copying resources files to build cluster directory...</echo>
<mkdir dir="${cluster}/resources"/>
<copy todir="${cluster}/resources">
<fileset dir="resources" includes="**"/>
</copy>
</target>
Then I create a new directory in my module's top directory (right alongside src, release, build) called 'resources' and place my .txt files in there.
然后我在模块的顶级目录(在 src、release、build 旁边)创建一个名为“resources”的新目录,并将我的 .txt 文件放在那里。
When you do a build on this module, netbeans-extra will get called as a dependency and carry out the creation of a 'resources' folder in the main project build/cluster directory, followed by copying the contents of the project resources directory over there.
在此模块上进行构建时,netbeans-extra 将作为依赖项被调用,并在主项目 build/cluster 目录中创建“resources”文件夹,然后将项目资源目录的内容复制到那里.
Ultimately, when you build a distribution for your project, you'll find the resource directory placed right next to your projects modules directory, making for a nice and neat side by side arrangement.
最终,当你为你的项目构建一个发行版时,你会发现资源目录就在你的项目模块目录旁边,从而形成一个漂亮而整洁的并排排列。
回答by Mata
Correct code...
正确的代码...
<target name="-pre-jar">
<echo>Copying resources files to build directory...</echo>
<mkdir dir="${dist.jar.dir}/resources"/>
<copy todir="${dist.jar.dir}/resources">
<fileset dir="resources" includes="**"/>
</copy>
</target>
Add this in the main build.xml (not nbproject\build-impl.xml). You may also replace "-pre-jar" with "-post-jar"
将此添加到主 build.xml(不是 nbproject\build-impl.xml)中。您也可以将“-pre-jar”替换为“-post-jar”

