2012-11-16 23 views
0

我有一个包含多个项目的工作区。所有这些都是Maven项目。其中一个项目的目标目录在构建后包含一个批处理文件。现在,我需要工作区中的其他项目之一来运行此批处理文件。所以,我想以编程方式获取当前工作区的路径,而不需要引入新的依赖关系来完成此任务。有没有人知道一种方法来做到这一点?需要在Eclipse工作区内运行批处理文件

编辑1:我在工作区中有一个父Maven项目。它的子目标目录之一是批处理文件。父级的另一个孩子(这是一个测试项目)需要运行该批处理文件。我可以使用Maven的basedir变量来获取批处理文件,如果我使用Eclipse运行单独的测试,那么这个文件不是很漂亮,不起作用。所以我想避免这个解决方案。

+1

也许没有什么帮助,但我的建议:博恩奈特该批处理文件。坚持Maven/Ant。 – artbristol

+0

@artbristol burninate? – badgerduke

+0

对不起。我的意思是删除,摆脱。如果你使用Maven/Ant,你可以保持跨平台和声明性质。批处理文件很笨重。 – artbristol

回答

0

您将遇到的问题是Eclipse中的项目不一定存储在工作空间目录中;他们可能在文件系统的任何地方。这意味着只需知道工作区的位置并不一定能帮助您找到批处理文件。

例如:我的工作区是$HOME/workspace,但我所有的项目(工作副本)都在$HOME/code/project。能够确定工作区并不是非常有用。项目可以存在于工作区之外,并且仍然在Eclipse中使用文件 - >导入

最好的办法是使用build-helper-maven-plugin的attach-artifact目标将批处理文件“附加”到构建版本。有一个如何做到这一点的例子here

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>build-helper-maven-plugin</artifactId> 
    <version>1.7</version> 
    <executions> 
     <execution> 
     <id>attach-artifacts</id> 
     <phase>package</phase> 
     <goals> 
      <goal>attach-artifact</goal> 
     </goals> 
     <configuration> 
      <artifacts> 
      <artifact> 
       <file>script.bat</file> 
       <type>bat</type> 
      </artifact> 
      </artifacts> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 

然后,你的其他项目可以使用Maven的依赖,插件的copy goal解决脚本到它自己的目录,并从那里运行它。

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-dependency-plugin</artifactId> 
    <version>2.5.1</version> 
    <executions> 
     <execution> 
     <id>copy</id> 
     <phase>package</phase> 
     <goals> 
      <goal>copy</goal> 
     </goals> 
     <configuration> 
      <artifactItems> 
      <artifactItem> 
       <groupId>...</groupId> 
       <artifactId>...</artifactId> 
       <version>...</version> 
       <type>bat</type> 
       <overWrite>true</overWrite> 
      </artifactItem> 
      </artifactItems> 
      <outputDirectory>${project.build.directory}/scripts</outputDirectory> 
      <overWriteSnapshots>true</overWriteSnapshots> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 
+0

感谢您提出这些建议。我会让你知道我采取了什么方法。 – badgerduke

0

maven-antrun-plugin怎么样?这不是很漂亮,但它能够完成任务:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-antrun-plugin</artifactId> 
    <version>1.7</version> 
    <executions> 
     <execution> 
      <id><!-- insert an id --></id> 
      <phase><!-- insert a maven lifecycle phase --></phase> 
      <configuration> 
       <tasks> 
        <exec 
         dir="${basedir}" 
         executable="${basedir}/src/main/sh/your-script.sh" 
         failonerror="true"> 
         <arg line="arg1 arg2 arg3" /> 
        </exec> 
       </tasks> 
      </configuration> 
      <goals> 
       <goal>run</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 

编号:http://maven.apache.org/plugins/maven-antrun-plugin/usage.html

+0

也许编辑来解决这个需求:“我需要工作区中的其他*项目之一来运行这个批处理文件”? –

+0

好的,所以它会变得有点混乱,但你可以做的是将脚本作为依赖包含在项目中,使用[maven-dependency-plugin](http://maven.apache.org/plugins/maven -dependency-plugin /)改为[unpack-dependencies](http://maven.apache.org/plugins/maven-dependency-plugin/examples/unpacking-project-dependencies.html),然后使用maven-antrun-插入。 – matsev

+0

@ Martin Ellis - 编辑我的问题,谢谢。 – badgerduke