使用 Ant 编译源代码树的选择性部分

发布于 2024-11-29 11:48:54 字数 999 浏览 2 评论 0原文

我的情况有点有趣。我正在尝试使用 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 技术交流群。

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

发布评论

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

评论(3

三寸金莲 2024-12-06 11:48:54

我不知道如何使用 Ant 的内置 选择器 来实现这一点。

但是,如果您无法以任何其他方式解决此问题,您可以实现 自定义选择器检查.java文件是否有相应的.hbm.xml文件,例如

public class CorrespondingHbmSelector implements FileSelector {
  public boolean isSelected(File basedir, final String filename, File f) {
    Pattern p = Pattern.compile("^(.+).java$");
    Matcher m = p.matcher(filename);
    boolean matches = m.matches();
    if (!matches) {
      return false;
    }
    final String basename = m.group(1);
    String[] foundFiles = basedir.list(new FilenameFilter() {
      @Override
      boolean accept(File dir, String name) {
        return name.startsWith(basename) && name.endsWith(".hbm.xml");
      }
    });
    return foundFiles.length > 0;
  }
}

在您的build.xml中使用它就像 include

<typedef
  name="correspondinghbmselector"
  classname="CorrespondingHbmSelector"
  classpath="${mydomain.classes}"/>

<javac srcdir="${source.dir}" ...>
  <correspondinghbmselector/>
</javac>

这只是基本的 主意。 (我没有测试代码。)

更好的方法是避免构建中出现如此复杂的依赖关系。

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.

public class CorrespondingHbmSelector implements FileSelector {
  public boolean isSelected(File basedir, final String filename, File f) {
    Pattern p = Pattern.compile("^(.+).java$");
    Matcher m = p.matcher(filename);
    boolean matches = m.matches();
    if (!matches) {
      return false;
    }
    final String basename = m.group(1);
    String[] foundFiles = basedir.list(new FilenameFilter() {
      @Override
      boolean accept(File dir, String name) {
        return name.startsWith(basename) && name.endsWith(".hbm.xml");
      }
    });
    return foundFiles.length > 0;
  }
}

Use it in your build.xml like an include

<typedef
  name="correspondinghbmselector"
  classname="CorrespondingHbmSelector"
  classpath="${mydomain.classes}"/>

<javac srcdir="${source.dir}" ...>
  <correspondinghbmselector/>
</javac>

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.

兔姬 2024-12-06 11:48:54

我认为你应该考虑将不同类型的类分成单独的项目。除了使打包过程更简单之外,还有其他好处:解耦、强制执行单向编译时依赖项等。

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.

滥情空心 2024-12-06 11:48:54

假设 Contact.java 不依赖 AnotherClass.java,这是可行的。使用 选择器子元素(在 Ant 文档的“概念和类型”下链接) )在您的 javac 标记中。

下面是一个示例:

    <javac srcdir="${source.dir}" destdir="${hibernate.dir}">
      <filename name="Contact.Java" />
    <javac>

当然,除了 选择器,您也可以直接使用 patternset 子元素和属性:

    <javac srcdir="${source.dir}" destdir="${hibernate.dir}">
      <include name="Contact.Java" />
    <javac>

或者

    <javac srcdir="${source.dir}" destdir="${hibernate.dir}"
           includea="Contact.Java" />

顺便说一下,命名约定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:

    <javac srcdir="${source.dir}" destdir="${hibernate.dir}">
      <filename name="Contact.Java" />
    <javac>

Of course, instead of a <filename> selector you can also directly use the patternset subelements and attributes:

    <javac srcdir="${source.dir}" destdir="${hibernate.dir}">
      <include name="Contact.Java" />
    <javac>

or

    <javac srcdir="${source.dir}" destdir="${hibernate.dir}"
           includea="Contact.Java" />

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.)

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