如何在 Eclipse 项目中构建期间过滤资源? [英] How to filter ressources during build in Eclipse project?

查看:28
本文介绍了如何在 Eclipse 项目中构建期间过滤资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用多个配置文件的应用程序(这里只考虑 appli.properties).这些文件包含多个取决于环境的值.我们可以找到一些信息,例如:

I have an application that uses several configuration files (let just consider appli.properties here). These files contain several values that depend on the environment. We can find some information such as:

server.port=${envi.server.port}

另一方面,我有一组属性文件,每个环境一个(dev.properties、homolo.properties 等).它们包含配置文件中某些属性的值.我们可以在这里找到这种属性:

On other side, I have a set of properties files, one per environment (dev.properties, homolo.properties, etc.). They contain the values for some properties in configuration files. We can find here this kind of properties:

envi.server.port=4242

我的构建由 Maven2 处理.一切正常.

My build is handled by Maven2. Everything is working fine.

但是,我现在需要将我的项目导入 Eclipse.我主要关心的是配置文件过滤.事实上,如果我没有修改我的项目的 Eclipse 参数中的任何内容(在 mvn eclipse:eclipse 命令之后),那么我的所有配置文件都将保留属性键(即 ${envi.server.port}) 而不是它们的值.有了这样的配置文件,我的应用程序将无法在 Eclipse 中运行...

However, I now need to import my project into Eclipse. My main concern is about the configuration files filtering. Indeed, if I do not modify anything in my Eclipse parameter for my project (after a mvn eclipse:eclipse command), then all my configuration file will keep the property keys (i.e. ${envi.server.port}) instead of their values. And with such configuration files, my application will not run inside Eclipse...

所以我尝试了两种解决方案:

So I tried two solutions:

  • 完整的 Maven 解决方案,使用 m2eclipse 插件.我在项目配置中添加了一个 Maven Builder,然后,每次构建时,都会对文件进行过滤.
  • Ant(仅在 Eclipse 中使用).我几乎定义了一个模拟Maven2文件过滤的任务在蚂蚁.此任务仅用于过滤,不用于编译.
  • A full-Maven solution, using m2eclipse plugin. I add a Maven Builder in the project configuration, and then, each time a build is made, the filtering is done on the files.
  • Ant (which is only used inside Eclipse). I've hardly defined a task that simulates the Maven2 filtering of files in Ant. This task is only dedicated to the filtering, no compilation.

这两种解决方案的共同问题是每次操作都要进行过滤(本质上节省了Java类版本),然后需要时间.然而,第二个解决方案比第一个(超过 10 秒)更快(3 秒).

The common problem of these two solutions is that the filtering is made at every operation (essentially saves on Java class edition), and then take time. The second solution is however quicker (3 seconds) than the first one (more than 10 seconds).

你觉得我的方法怎么样?你会如何以更好的方式做到这一点?

What do you think of my approach? How would you do that, in a better way?

推荐答案

如果资源不是经常改变,你可以将 Maven 构建设置为只在 Clean 构建后运行,那么它不会干扰这么多,这个然而,并没有做任何事情来加速构建.

If the resources are not changed that often, you can set the Maven build to only run after a Clean build, then it won't interfere so much, this doesn't do anything to speed up the build however.

至于使过滤更快,我不知道有任何其他简单的机制可以提供帮助,正如您所说,您需要 Ant 或 Maven 来运行过滤,而且它们都需要一些时间来设置在构建之前,导致速度变慢.

As far as making the filtering quicker, I don't know of any other simple mechanism that will help, as you've said you need either Ant or Maven to run the filtering, and they both take some time to set up before building, resulting in the slow down.

如果这给你带来了很多问题,你可以编写一个自定义的对增量执行过滤的增量 Eclipse 构建器.这应该会快得多,但显然要付出更多的努力.

If this is causing you a lot of problems, you can write a custom Incremental Eclipse builder that performs the filtering on the deltas. This should be considerably quicker, but obviously a lot more effort to write.

这篇关于如何在 Eclipse 项目中构建期间过滤资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆