Maven exec无法将结果写入输出目录

时间:2014-08-12 05:06:58

标签: java maven

我在pom.xml中使用以下配置将所有maven依赖项捆绑在一个jar中。当我运行mvn exec命令时,它工作正常,除了它没有将结果写入我已配置为在调用main方法时存储结果的输出目录。 如果我从命令行运行相同的程序并传递参数,则输出目录具有结果,但它不能从Maven运行。我错过了什么?有人可以帮忙吗?

<build>
    <plugins>
      <plugin>
      <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.3.2</version>
        <executions>
            <execution>
                <goals>
                    <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>java</executable>
                    <arguments>
                        <argument>-classpath</argument>
                        <classpath />
                        <argument>com.myapp.test.AppLauncher</argument>
                        <argument>-a</argument>
                        <argument>${Param1}</argument>
                        <argument>-b</argument>
                        <argument>${Param2}</argument>
                    </arguments>
                </configuration>
      </plugin>
    </plugins>
  </build>

这是maven命令的结果 -

$mvn exec:exec -DParam1=DefaultMode -DParam2=Test
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building myApp
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- exec-maven-plugin:1.3.2:exec (default-cli) @ myApp ---
Fetching results from database
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.943 s
[INFO] Finished at: 2014-08-11T21:56:30-07:00
[INFO] Final Memory: 13M/310M
[INFO] ------------------------------------------------------------------------

2 个答案:

答案 0 :(得分:0)

<configuration>元素中,添加<workingDirectory>target/output</workingDirectory>之类的内容。 Here就是一个例子。

答案 1 :(得分:0)

您可以尝试显式传递目标目录,也值得添加执行阶段。 maven插件配置如下。

<build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>java</executable>
                    <arguments>
                        <argument>-classpath</argument>
                        <classpath/>
                        <argument>com.kp.deployment.CopyResourceFiles</argument>
                        <argument>destinationDirectory=${project.build.directory}</argument>
                        <argument>param1=${param1}</argument>
                        <argument>param2=${param2}</argument>
                    </arguments>
                </configuration>
            </plugin>
        </plugins>
    </build>

hava类如下

public class CopyResourceFiles {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {


        String destionDir = "";
        String param1 ="";
        String Param2 ="";
        for(String str : args){

            if(str != null && str.contains("destinationDirectory=")){
                destionDir = str.substring(str.indexOf("=")+1, str.length());
            }
            if(str != null && str.contains("param1=")){
                param1 = str.substring(str.indexOf("=")+1, str.length());
            }
            if(str != null && str.contains("param2=")){
                Param2 = str.substring(str.indexOf("=")+1, str.length());
            }
        }
        destionDir+="/kpfile.txt";

        List<String> contents= new ArrayList<String>();
        contents.add(param1);
        contents.add(Param2);
        FileUtils.writeLines(new File(destionDir), contents);

        System.out.println("Sucessfully written");

    }

}