在 Eclipse 中使用 Apache Ant 修改项目构建路径

发布于 2024-11-01 04:30:05 字数 462 浏览 3 评论 0原文

我有一个 ant 脚本,它通常执行以下操作:clean、compile、jar、javadocs 等。我在 Eclipse 中还有 3 个项目:一个库和 2 个用于测试它的项目。

在这两个测试项目的构建路径中,我将位于库项目中的库 jar 定义为外部 jar。

库jar 在jar 名称中包含其版本,即library-0.1.jar。在 ant 脚本中,我有一个包含库版本的属性:

<property name="project_version" value="0.1"/>

因此,要更改版本,我修改此属性并再次运行脚本。正如您可能推断的那样,这会在其他 2 个项目中生成依赖项错误,因为它们仍然指向旧文件library-0.1.jar。

如何在 Eclipse 中自动更改另外 2 个项目的构建路径? Apache ant 可以使用特定标签来做到这一点吗?

谢谢。

I have an ant script that does the tipically: clean, compile, jar, javadocs, etc. I also have 3 projects in Eclipse: a library and 2 projects to test it.

In the build path of this 2 test projects I've defined as external jar the library jar located in the library project.

The library jar has its version in the jar name, i.e. library-0.1.jar. In the ant script I have a property with the version of the library:

<property name="project_version" value="0.1"/>

So to change the version I modify this property and run the script again. As you may deduce this generates a dependency error in the 2 other projects because they will still be pointing to an old file library-0.1.jar.

How can I change automatically the build path of that 2 other projects in Eclipse? Apache ant can do this with a specific tag?

Thanks.

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

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

发布评论

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

评论(3

八巷 2024-11-08 04:30:05

在所有构建文件中引用带有变量的版本,例如

<include name="my-${version}.jar"/>

现在,当您执行构建时,您可以使用显式版本来执行以匹配您的需求,例如,

ant -Dversion=1.3

或者,您可以在每个构建脚本中加载相同的属性文件加载版本属性

<property file="version.properties">

请注意,如果您选择后者,您应该删除显式设置值的属性声明(从上面的帖子中)。要么先加载属性文件。
....

Refer to the version with a variable in all your build files, e.g.

<include name="my-${version}.jar"/>

Now when you execute your builds, you can execute with explict version to match what you require, e.g.

ant -Dversion=1.3

Alternatively, you could load the same properties file in each of your build scripts to load the version property

<property file="version.properties">

Note that if you go with the latter you should remove the property declaration (from you post above) which sets the value explicitly. Either that or load the properties file first.
....

不乱于心 2024-11-08 04:30:05

使用 sed,例如(未测试):

version=2
sed s/value="\d+"/value="$version"/g build.xml

Use sed, e.g. (not tested):

version=2
sed s/value="\d+"/value="$version"/g build.xml
伴我心暖 2024-11-08 04:30:05

编辑:使用下面的方法,您将能够使用 Ant 进行编译,但是 Eclipse 会在项目资源管理器中向您显示依赖项错误,因为您没有在构建路径面板中定义任何外部 jar。要解决此问题,您必须编辑将在项目根目录中看到的 .classpath 文件并添加以下行:

<classpathentry kind="lib" path="../Library/bin"/>

其中 Library 是库项目的文件夹, bin 是类的文件夹。


已解决

我必须为另外 2 个项目编写一个 ant 脚本,并使用该脚本设置类路径,而不是使用 eclipse IDE:

<path id="build-classpath">
    <fileset dir="${dist}">
        <include name="${project_name}-${project_version}.jar"/>
    </fileset>
</path>

${dist} 是 jar 库所在的文件夹,如下所示: “../Library/dist”,其中 Library 是项目的名称。
${project_name} 和 ${project_version} 从 version.properties 文件加载,该文件再次存储在“../Library”中:

<property file="..Library/version.properties"/>

文件 version.properties 仅包含:

project_name=LibraryName
project_version=0.1

然后,在编译时添加类路径...

<target name="compile" depends="clean, makedir">
    <javac srcdir="${src}" destdir="${bin}">
        <classpath refid="build-classpath"/>
    </javac>
</target>

refid 值是之前定义的路径 id。


EDIT: Using the method below you will be able to compile using Ant, but eclipse will show you a dependency error in the project explorer because you don't have defined any external jar in the build path panel. To solve this you have to edit the .classpath file that you will see in the project root and add the following line:

<classpathentry kind="lib" path="../Library/bin"/>

Where Library is the folder for Library project and bin the folder for classes.


SOLVED:

I have to write an ant script for the 2 other projects and set the classpath with the script, not with eclipse IDE:

<path id="build-classpath">
    <fileset dir="${dist}">
        <include name="${project_name}-${project_version}.jar"/>
    </fileset>
</path>

${dist} is the folder where is the jar library it's something like: "../Library/dist", where Library is the name of the project.
${project_name} and ${project_version} are loaded from a version.properties file that, again, is stored in "../Library":

<property file="..Library/version.properties"/>

The file version.properties just contains:

project_name=LibraryName
project_version=0.1

Then, to add the classpath when compiling...

<target name="compile" depends="clean, makedir">
    <javac srcdir="${src}" destdir="${bin}">
        <classpath refid="build-classpath"/>
    </javac>
</target>

The refid value is the path id defined previously.

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