使用 Ant 编译源代码树的选择性部分
我的情况有点有趣。我正在尝试使用 Ant 制作 jar 文件。但是,我不希望编译每个 java 文件。我的源代码具有如下结构:
src
-Contact.Java
-Contact.hbm.xml
-AnotherClass.Java
因此,我想要在 jar 文件中包含如下内容:
myjar.jar
-com
--me
---Contact.Class
---Contact.hbm.xml
我不希望 AnotherClass.Class 在那里。
有可能用 Ant 做到这一点吗?
我想出的最好的办法是这样的:
<target name="condition-hibernate" description="check if there is a java and hbm file then call for compile-hibernate">
<condition property="condition">
<available file="**/*.hbm.xml" />
</condition>
<antcall target="condition-hibernate-part2"/>
</target>
<target name="condition-hibernate-part2" description="check if there is a java and hbm file then call for compile-hibernate">
<javac srcdir="${source.dir}" destdir="${hibernate.dir}">
</target>
我不认为我可以使用 Ant 来解决我的问题,因为我无法告诉 Ant 编译什么和不编译什么(我可以使用排除和包含),但我想告诉Ant 仅编译旁边有 *.hbm.xml 文件的文件。
My situation is somehow interesting. I am trying to use Ant to make a jar file. However, I don't want every single java file to be compiled. My source code has strcuture something like this:
src
-Contact.Java
-Contact.hbm.xml
-AnotherClass.Java
So, what I want to have in my jar file is as follow:
myjar.jar
-com
--me
---Contact.Class
---Contact.hbm.xml
I don't want AnotherClass.Class to be there.
Is it possible doint that with Ant ?
The best I come up with is like this:
<target name="condition-hibernate" description="check if there is a java and hbm file then call for compile-hibernate">
<condition property="condition">
<available file="**/*.hbm.xml" />
</condition>
<antcall target="condition-hibernate-part2"/>
</target>
<target name="condition-hibernate-part2" description="check if there is a java and hbm file then call for compile-hibernate">
<javac srcdir="${source.dir}" destdir="${hibernate.dir}">
</target>
I don't think that I can use Ant to solve my problem as I can't tell Ant what to compile and what not (I can using exclude and include) but I want to tell Ant compile only files that have an *.hbm.xml file next to them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道如何使用 Ant 的内置 选择器 来实现这一点。
但是,如果您无法以任何其他方式解决此问题,您可以实现 自定义选择器检查
.java
文件是否有相应的.hbm.xml
文件,例如在您的
build.xml
中使用它就像include
这只是基本的 主意。 (我没有测试代码。)
更好的方法是避免构建中出现如此复杂的依赖关系。
I don't know how to achieve this with Ant's built-in selectors.
However, if you cannot work around this in any other way you could implement a custom selector that checks if the
.java
file has a corresponding.hbm.xml
file, e.g.Use it in your
build.xml
like aninclude
This is just the basic idea. (I didn't test the code.)
The better way would be to avoid such complex dependencies in your build.
我认为你应该考虑将不同类型的类分成单独的项目。除了使打包过程更简单之外,还有其他好处:解耦、强制执行单向编译时依赖项等。
I think you should consider separating the different types of classes into separate projects. Apart from making your packaging process simpler, there are other benefits too: decoupling, enforcing one-way compile-time dependencies etc.
假设 Contact.java 不依赖 AnotherClass.java,这是可行的。使用 选择器子元素(在 Ant 文档的“概念和类型”下链接) )在您的
javac
标记中。下面是一个示例:
当然,除了
选择器,您也可以直接使用patternset
子元素和属性:或者
顺便说一下,命名约定Java 的文件扩展名仅使用小写字母,即
.java
,而不是.Java
(和.class
)。如果您使用的是像大多数 Windows 文件系统一样不区分大小写的文件系统,这可能并不重要,但您仍然应该这样做,以避免以后出现意外。)Assuming Contact.java is not depending on AnotherClass.java, it is doable. Use selector sub-elements (linked in the Ant documentation under "concepts and types") in your
javac
tag for this.Here is an example:
Of course, instead of a
<filename>
selector you can also directly use thepatternset
subelements and attributes:or
By the way, the naming convention for Java's file extensions is using only lower case letters, i.e.
.java
, not.Java
(and.class
). If you are on a case-insensitive file system like most Windows file systems, this might not matter, but you should still do it, to avoid surprises later.)