Maven:如何打包和部署main()方法执行生成的输出文件?

时间:2017-03-08 01:01:22

标签: maven jar maven-assembly-plugin exec-maven-plugin

我有一个Maven项目使用exec-maven-plugin来执行带有main方法的类,这会在目标目录中生成一个输出文件。配置如下所示:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.5.0</version>
    <executions>
        <execution>
            <id>process-execution</id>
            <phase>package</phase>
            <goals>
                <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <mainClass>com.example.MainClass</mainClass>
        <systemProperties>
            <systemProperty>
                <key>INPUT_FILE_PATH</key>
                <value>${basedir}/src/main/resources/input_file.csv</value>
            </systemProperty>
            <systemProperty>
                <key>OUTPUT_FILE_PATH</key>
                <value>${project.build.directory}/output_file.json</value>
            </systemProperty>
        </systemProperties>
    </configuration>
</plugin>

我希望能够将此输出文件(output_file.json)作为单独的jar打包并部署到包存储库以及使用项目类构建的标准jar文件。

有没有办法完成这项工作?也许是maven-assembly-plugin

1 个答案:

答案 0 :(得分:1)

Yes, you can install and deploy an additional artifact by using the maven-assembly-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
      <execution>
        <id>create-distribution</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
        <configuration>
          <descriptors>
            <descriptor>src/assembly/descriptor.xml</descriptor>
          </descriptors>
        </configuration>
      </execution>
    </executions>
 </plugin>

This means that an additional artifact according to "descriptor.xml" is created, installed and deployed. The file "descriptor.xml" defines which directory should be packaged:

<?xml version="1.0" encoding="UTF-8"?>
<assembly
  xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
    http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2
      http://maven.apache.org/xsd/assembly-1.1.2.xsd"
>
  <id>json</id>
  <formats>
    <format>jar</format>
  </formats>
  <fileSets>
    <fileSet>
      <outputDirectory>/</outputDirectory>
      <directory>/target/deploy/json</directory>
    </fileSet>
  </fileSets>
</assembly>