使用 java、eclipse 和 ant 自动复制构建的文件和库 [英] Automatically Copy Built Files and Libraries with java, eclipse, and ant

查看:25
本文介绍了使用 java、eclipse 和 ant 自动复制构建的文件和库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 7 上使用 java 和 eclipse,我正在尝试构建一个服务器程序,该程序将在我网络上的另一台 (ubuntu) 机器上运行.

I'm using java with eclipse on windows 7, and I'm trying to build a server program that will run on another (ubuntu) machine on my network.

如何让 eclipse 将构建的类文件保存到外部位置(我有一个驱动器映射,或者它甚至可以通过 ssh/sftp/什么)?

How can I get eclipse to save the built class files into an external location (I have a drive mapped, or it could even be over ssh/sftp/whatever)?

我已经在那台机器上创建了一个 git 存储库,所以类文件保存在那里,但是我正在使用的外部库(在构建路径上)没有被保存为存储库的一部分.理想情况下,我希望 eclipse 自动将使用的构建类和库复制到另一个目录中(更理想的是,当我单击运行"时,让它自动运行/终止程序,尽管我怀疑这可能有点太乐观).

I have created a git repository on that machine, so the class files are saved in there, however the external libraries that I'm using (which are on the build path) are not being saved as part of the repository. Ideally I would like to have eclipse copy the built classes and libraries used into another directory automatically (and even more ideally get it to run/kill the program automatically when I click on "run", although I suspect that might be being a little too optimistic).

我认为与 Eclipse 一起安装的 ant 可以做到这一点,但我不确定如何实现.

I think ant which I believe is installed with eclipse can do this, but I'm not sure how to make it happen.

推荐答案

你提到了 jar 文件,它们实际上是依赖项.如果您真的不想将这些 jar 文件包含在您的存储库中,您可能需要查看一些依赖管理系统,如 Ivy,或者切换到 Maven.

You mentioned jar files, and they are actually dependencies. You may want to take a look at some dependency management system like Ivy, or switch to Maven, if you really don't want to include those jar files in your repository.

或者,您始终可以使用 Ant 的构建脚本"复制依赖项(尽管它们是 XML,而不是真正的脚本").

And alternatively, you are always able to copy the dependencies with Ant's build "scripts" (although they are XMLs, not really "scripts").

如何让 eclipse 将构建的类文件保存到外部位置(我有一个驱动器映射,或者它甚至可以通过 ssh/sftp/什么)?

How can I get eclipse to save the built class files into an external location (I have a drive mapped, or it could even be over ssh/sftp/whatever)?

很难想出一种方法来解决如何将它们保存到外部位置".但是,您可以只编写一个目标,使用 任务(基于 ssh)或 任务,在运行标准构建后复制文件.

It's hard to think of a way to solve "how to save them into an external location". However, you can just write a target, using <scp> task (based on ssh) or <ftp> task, to copy the files after running the standard building.

理想情况下,我希望 eclipse 自动将使用的构建类和库复制到另一个目录中.

Ideally I would like to have eclipse copy the built classes and libraries used into another directory automatically.

Eclipse 内置了对 Ant 的支持.我不使用 Eclipse,但我使用 Netbeans.在 Netbeans 中,项目实际上是使用 Netbeans 生成的 Ant 构建文件构建的.并且,您可以自定义构建文件,添加您自己的目标,然后您可以在 IDE 中运行您自己的目标.

Eclipse has built-in support for Ant. I don't use Eclipse, but I use Netbeans. In Netbeans, a project is actually built with the Ant build file generated by Netbeans. And, you can customize the build file, add your own targets, and then, you can run your own target inside the IDE.

例如,您有一个 build 目标、一个 deploy 目标和一个 run 目标.第一个是 Eclipse 生成的,你不需要关心它,而后两个是你自己写的.再写一个target,说all,用把上面的三个target一个接一个运行.然后,在 Eclipse 中,您可以直接运行 all target 来获取 Ant 处理的所有内容.

For example, you have a build target, a deploy target, a run target. The first is generated by Eclipse and you don't need to care about it, while the latter two are written by you. Just write another target, say all, and use <antcall> to run the three targets above one after another. And then, in Eclipse, you can directly run all target to get all the things handled by Ant.

更理想的是让它在我点击运行"时自动运行/终止程序,尽管我怀疑这可能有点过于乐观了.

even more ideally get it to run/kill the program automatically when I click on "run", although I suspect that might be being a little too optimistic.

目标机器是Linux机器,所以你可以用apt-get轻松安装SSH服务器.在你的 ant 构建文件中写入两个目标,一个是 run,另一个是 kill.

The target machine is a Linux machine, so you can install SSH server easily with apt-get on it. Write two targets in your ant build file, one is run, and the other is kill.

run 目标中,您可以使用 来运行您的应用程序.您唯一需要注意的是,该机器上定义的环境变量可能在 ssh 会话中不可用.但这只是一个配置问题.至少您可以将变量放在 /etc/environment 中,尽管这不是最佳做法.

Inside the run target, you can use <sshexec> to run your app. The only thing you need to take care is the environment varibles defined on that machine may not be available in ssh session. But that's just a configuration problem. At least you can put the varibles in /etc/environment, although that's not the best practice.

kill 目标可能稍微复杂一些.但是,在我看来,如果你可以写一个 startup.sh 来启动一个应用程序,你总是可以写一个 stop.sh 来关闭它(看看 Tomcat 的catalina.sh).

The kill target may be a little more complicated. However, in my opinion, if you can write a startup.sh to start an app, you can always write a stop.sh to close it (take a look at Tomcat's catalina.sh).

此外,可以在 Eclipse 中调用 kill 目标.

And also, the kill target can be called inside your Eclipse.

这篇关于使用 java、eclipse 和 ant 自动复制构建的文件和库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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