javac选项以递归方式编译给定目录下的所有Java文件 [英] javac option to compile all java files under a given directory recursively

查看:4556
本文介绍了javac选项以递归方式编译给定目录下的所有Java文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用javac编译器在我的项目中编译java文件。这些文件分布在几个软件包中,例如: com.vistas.util com.vistas.converter com.vistas.LineHelper com.current.mdcontect



每个这些软件包都有几个java文件。我使用javac像这样:

  javac com / vistas / util / vistas / converter / *。java 
com.vistas.LineHelper / *。java com / current / mdcontect / *。java

(在一行中)



我们可以要求编译器递归地编译所有的java文件parent com目录?

解决方案

我还建议使用某种构建工具( Ant Maven ,Ant已经建议并且更容易开始)或者一个处理编译的IDE(Eclipse使用增量编译与调和策略,你甚至不必关心任何 Compile按钮)。



使用Javac



一个更大的项目,没有任何正确的构建工具附近,你可以总是使用一个小技巧, javac 提供:可以在一个文件中指定编译的类名。您只需将文件名以 @ 前缀传递给 javac



如果您可以在项目中创建所有 *。java 文件的列表,很容易:

 #Linux 
$ find -name* .java> sources.txt
$ javac @ sources.txt

:: Windows
> dir / s / B * .java> sources.txt
> javac @ sources.txt




  • 是您必须重新生成 sources.txt 文件每次创建一个新的源或重命名现有的一个文件,这是一个容易忘记(因此容易出错)和令人讨厌的任务。


使用构建工具



从长远来看,最好使用用于构建软件的工具。



使用Ant



如果您创建一个简单的 build.xml 文件构建软件:

 < project default =compile> 
< target name =compile>
< mkdir dir =bin/>
< javac srcdir =srcdestdir =bin/>
< / target>
< / project>

您可以通过运行以下命令编译整个软件:

  $ ant 




  • 的优点是您使用的是易于扩展的标准构建工具。

  • 必须下载,设置和学习一个额外的工具。请注意,大多数IDE(如NetBeans和Eclipse)对编写构建文件提供了极大的支持,因此您不必在此情况下下载任何内容。



使用Maven



Maven不是那么容易设置和使用,但学习得很好。以下是 5分钟内启动项目的精彩教程。




  • 这是主要优点(对我来说),它处理依赖关系,所以你不需要下载任何更多的Jar文件,并手工管理,我发现它更有用的建设,包装和测试更大的项目。

  • 缺点曲线,如果Maven插件喜欢抑制错误:-)另一件事是,相当多的工具也与Maven存储库(如 Sbt 适用于Scala, Ivy 适用于Ant,
  • 现在,什么可以提高你的开发生产力。有几个开源替代品(如 Eclipse NetBeans a>,我更喜欢使用前者),甚至是非常受欢迎和功能强大的商业版本(例如 IntelliJ )。



    他们可以在后台管理项目构建,所以你不必处理所有的命令行的东西。但是,如果你知道在后台中实际发生了什么,那么它总是很方便,所以你可以像下面这样搜索错误: ClassNotFoundException



    另外一个注意



    对于大型项目,建议使用IDE 构建工具。前者提高了你的生产力,而后者使得可以在项目中使用不同的IDE(例如,Maven可以用一个简单的 mvn eclipse:eclipse 命令生成Eclipse项目描述符) 。此外,具有可以使用单线命令测试/构建的项目易于向新同事或例如连续集成服务器介绍。蛋糕: - )


    I am using the javac compiler to compile java files in my project. The files are distributed over several packages like this: com.vistas.util, com.vistas.converter, com.vistas.LineHelper, com.current.mdcontect.

    Each of these packages has several java files. I am using javac like this:

    javac com/vistas/util/*.java com/vistas/converter/*.java
          com.vistas.LineHelper/*.java com/current/mdcontect/*.java
    

    (in one line)

    Instead of giving so many paths, how can I ask the compiler to compile recursively all the java files from the parent com directory?

    解决方案

    I would also suggest using some kind of build tool (Ant or Maven, Ant is already suggested and is easier to start with) or an IDE that handles the compilation (Eclipse uses incremental compilation with reconciling strategy, and you don't even have to care to press any "Compile" buttons).

    Using Javac

    If you need to try something out for a larger project and don't have any proper build tools nearby, you can always use a small trick that javac offers: the classnames to compile can be specified in a file. You simply have to pass the name of the file to javac with the @ prefix.

    If you can create a list of all the *.java files in your project, it's easy:

    # Linux
    $ find -name "*.java" > sources.txt
    $ javac @sources.txt
    
    :: Windows
    > dir /s /B *.java > sources.txt
    > javac @sources.txt
    

    • The advantage is that is is a quick and easy solution.
    • The drawback is that you have to regenerate the sources.txt file each time you create a new source or rename an existing one file which is an easy to forget (thus error-prone) and tiresome task.

    Using a build tool

    On the long run it is better to use a tool that was designed to build software.

    Using Ant

    If you create a simple build.xml file that describes how to build the software:

    <project default="compile">
        <target name="compile">
            <mkdir dir="bin"/>
            <javac srcdir="src" destdir="bin"/>
        </target>
    </project>
    

    you can compile the whole software by running the following command:

    $ ant
    

    • The advantage is that you are using a standard build tool that is easy to extend.
    • The drawback is that you have to download, set up and learn an additional tool. Note that most of the IDEs (like NetBeans and Eclipse) offer great support for writing build files so you don't have to download anything in this case.

    Using Maven

    Maven is not that trivial to set up and work with, but learning it pays well. Here's a great tutorial to start a project within 5 minutes.

    • It's main advantage (for me) is that it handles dependencies too, so you won't need to download any more Jar files and manage them by hand and I found it more useful for building, packaging and testing larger projects.
    • The drawback is that it has a steep learning curve, and if Maven plugins like to suppress errors :-) Another thing is that quite a lot of tools also operate with Maven repositories (like Sbt for Scala, Ivy for Ant, Graddle for Groovy).

    Using an IDE

    Now that what could boost your development productivity. There are a few open source alternatives (like Eclipse and NetBeans, I prefer the former) and even commercial ones (like IntelliJ) which are quite popular and powerful.

    They can manage the project building in the background so you don't have to deal with all the command line stuff. However, it always comes handy if you know what actually happens in the backround so you can hunt down ocassional errors like a ClassNotFoundException.

    One additional note

    For larger projects, it is always advised to use an IDE and a build tool. The former boosts your productivity, while the latter makes it possible to use different IDEs with the project (e.g., Maven can generate Eclipse project descriptors with a simple mvn eclipse:eclipse command). Moreover, having a project that can be tested/built with a single line command is easy to introduce to new colleagues and into a continous integration server for example. Piece of cake :-)

    这篇关于javac选项以递归方式编译给定目录下的所有Java文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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