0

我对 maven/ant 很陌生,因此,如果我的问题对您来说很愚蠢,请原谅...

在我正在处理的应用程序中,我必须初始化一个虚拟机(例如 Amazon EC2 实例),然后将 JAR 文件上传到该机器,最后运行它——每次我想运行它时,运行时参数可能不同。

这样做的最佳方法是什么?除了 JRE 之外,我还需要在该虚拟机上安装哪些框架/库(如 maven 或 ant)才能使其正常工作?

4

3 回答 3

1

如果你坚持Maven,你可以攻击jar上传过程install phase如下:

pom.xml:

...
<plugins>
...
  <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <configuration>
      <tasks>
        <scp todir="${targetNode}:~/" trust="true" failonerror="true"
          file="${project.build.directory}/${project.build.finalName}.jar" />
      </tasks>
    </configuration>
    <executions>
      <execution>
        <id>upload</id>
        <phase>install</phase>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
    <dependencies>
      <dependency>
        <groupId>org.apache.ant</groupId>
        <artifactId>ant-jsch</artifactId>
        <version>1.8.3</version>
      </dependency>
    </dependencies>
  </plugin>
...
</plugins>
...

然后发出:

mvn clean install "-DtargetNode=user:pass@machine"

(可选)编写一个脚本,在目标机器上运行上传的 jar。

于 2012-09-04T18:52:18.647 回答
0

You likely won't need Ant/Maven deployed remotely.

Can you instead ssh/scp onto that machine ? Ant has sshexec and scp tasks to allow you to copy and run stuff remotely.

于 2012-09-04T14:07:05.577 回答
0

最好的方法是创建满足您所有需要的 shell 脚本。如果机器是unix之类的,你可以用ssh、scp(使用ssh)来实现这一切。

使用 ssh,您可以执行远程 shell 命令。

示例(all_in_one.sh):

args=$1
arg2=$2
etc.
ssh admin@amazon_instance << end
/home/admin/bin/startAmazonVM.sh $arg1
end

scp mylocal.jar admin@amazon_instance:/home/admin/server/libraries/

ssh admin@amazon_instance << end
$JAVA_HOME/bin/java -jar /home/admin/server/libraries/mylocal.jar $arg2
end

然后您可以运行例如 all_in_one.sh(添加 >> output.txt 以创建执行输出文件,然后您可以对其进行解析以验证该过程)

如果您在 Windows 上工作,请安装 Cygwin 或 MinGW,或者使用 ant 并花更多时间在其中配置任务以完成其他工具最擅长的事情。

于 2012-09-04T14:39:02.223 回答