如何构建和分发我的 Java 项目?
我使用 Eclipse 构建了一个非常简单的 Java 项目。它在 IDE 中构建并运行。我有一些使用 JUnit 编写的单元测试。它们都在 IDE 中构建并传递。
我的项目位于以下路径:
/home/vg1890/workspace/project/
主要源代码位于:
/home/vg1890/workspace/project/src
测试位于:
/home/vg1890/workspace/project/tests
包名称为 com.vg1890.stuff。
当我在命令行输入: echo $CLASSPATH
时,没有返回任何内容(ubuntu)。
- 如何从 IDE 外部构建整个项目?
- 如何做到这一点,以便当我将源代码分发到另一台计算机时它将构建并运行(包括单元测试)?
谢谢!
I used Eclipse to build a pretty simple Java project. It builds and runs in the IDE. I have a few unit tests I wrote using JUnit. They all build and pass in the IDE.
My project is in the following path:
/home/vg1890/workspace/project/
The main source is in:
/home/vg1890/workspace/project/src
And the tests are in:
/home/vg1890/workspace/project/tests
The package name is com.vg1890.stuff
.
When I type: echo $CLASSPATH
at the command line, nothing is returned (ubuntu).
- How can I build the entire project from outside the IDE?
- How do make it so that when I distribute the source to another computer that it will build and run (including the unit tests)?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这正是 Apache Ant 通常使用的类型。 Eclipse 很好地支持ant 脚本。
This is exactly the kind of thing that Apache Ant is usually used for. Eclipse supports ant scripts quite well.
我建议使用 Apache Maven 来管理您的构建和分发源代码(maven 被广泛使用,并且,像 Ant 一样,很多 Java 用户/程序员都将其安装在他们的计算机上)。
很难用一行来定义 Maven(Maven 不仅仅是一个“构建工具”),因此我在下面粘贴了取自这本伟大书籍的第一章的更完整的定义:Maven:权威指南。
安装 Maven 后,为您的项目进行完整的构建(编译 java 源代码和单元测试、执行测试、打包已编译的类)只需 10 秒(真的)。
要开始使用 Eclipse,请查看 使用 Eclipse 的指南Maven 2.X。
I'd suggest to use Apache Maven to manage your build and distribute your sources (maven is widely used and, like Ant, lost of Java users/programmers have it installed on their computer).
It's hard to define maven in one line (maven is much more than just a "build tool") so I've pasted below a more complete definition taken from the first chapter of this great book: Maven: the Definitive Guide.
Once maven installed, putting a complete build in place for your project (compilation of java sources and unit tests, execution of tests, packaging of the compiled classes) is a matter of 10 seconds (really).
To get started with Eclipse, have a look at the Guide to using Eclipse with Maven 2.X.
根据 Michael 的回答,请注意,许多 IDE(IDEA、NetBeans、Eclipse)允许您将其构建机制与您自己的 Ant 构建脚本非常紧密地集成,并且可以帮助您启动它们。
刚开始时这可能很有用。然而,稍后考虑实现您的构建机制,以便它干净地工作(即与您的IDE完全分离)是有用的。如今,这是一个非常好的主意,IDE 通常对某些技术(例如 Spring、Hibernate)具有内置支持。否则,很容易发布一些缺少某些重要库的 JAR 文件,或者最终产生您不知道存在的依赖项。
Further to Michael's answer, note that many of the IDEs (IDEA, NetBeans, Eclipse) allow you to integrate their build mechanisms very tightly with your own Ant build scripts, and can give you a hand starting them.
This can be useful when starting out. However, it is useful later to consider implementing your build mechanism so that it works cleanly (i.e. completely separated from your IDE). This is a very good idea these days, where often IDEs have such in-built support for certain technologies (e.g. Spring, Hibernate). It is otherwise too easy to release some JAR file which is missing some vital library, or end up with a dependency that you didn't know existed.