如何构建和分发我的 Java 项目?

发布于 2024-08-03 21:56:31 字数 523 浏览 1 评论 0原文

我使用 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)。

  1. 如何从 IDE 外部构建整个项目?
  2. 如何做到这一点,以便当我将源代码分发到另一台计算机时它将构建并运行(包括单元测试)?

谢谢!

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

  1. How can I build the entire project from outside the IDE?
  2. 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 技术交流群。

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

发布评论

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

评论(3

睫毛溺水了 2024-08-10 21:56:31

这正是 Apache Ant 通常使用的类型。 Eclipse 很好地支持ant 脚本。

This is exactly the kind of thing that Apache Ant is usually used for. Eclipse supports ant scripts quite well.

゛清羽墨安 2024-08-10 21:56:31

我建议使用 Apache Maven 来管理您的构建和分发源代码(maven 被广泛使用,并且,像 Ant 一样,很多 Java 用户/程序员都将其安装在他们的计算机上)。

很难用一行来定义 Maven(Maven 不仅仅是一个“构建工具”),因此我在下面粘贴了取自这本伟大书籍的第一章的更完整的定义:Maven:权威指南

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.

Maven is a project management tool
which encompasses a project object
model, a set of standards, a project
lifecycle, a dependency management
system, and logic for executing plugin
goals at defined phases in a
lifecycle. When you use Maven, you
describe your project using a
well-defined project object model,
Maven can then apply cross-cutting
logic from a set of shared (or custom)
plugins.

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.

娇俏 2024-08-10 21:56:31

根据 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.

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