2013-04-03 83 views
18

我有一个Maven项目,我想用它创建两个可执行的JAR文件。一个将被用户以交互方式使用,另一个将作为预定作业运行,读取前者生成的日志文件。最后,我希望这两个jar文件是除了在MANIFEST.MF文件中的Main-Class属性相同。创建两个可执行罐子使用maven组件,插件

我使用maven-antrun-插件来创建一个可执行的JAR,这似乎是工作的罚款,直到我试图通过引入Maven的配置文件,以创建第二个jar文件。我的POM文件的相关部分如下所示:

<profiles> 
    <profile> 
     <id>publisher</id> 
     <build> 
      <finalName>${project.artifactId}</finalName> 
      <plugins> 
       ... 
       <plugin> 
        <artifactId>maven-assembly-plugin</artifactId> 
        <version>2.4</version> 
        <configuration> 
         <appendAssemblyId>false</appendAssemblyId> 
         <finalName>${project.artifactId}</finalName> 
         <archive> 
          <manifest> 
           <mainClass>fully.qualified.path.Publisher</mainClass> 
          </manifest> 
         </archive> 
         <descriptorRefs> 
          <descriptorRef>jar-with-dependencies</descriptorRef> 
         </descriptorRefs> 
        </configuration> 
        <executions> 
         <execution> 
          <phase>package</phase> 
          <goals> 
           <goal>single</goal> 
          </goals> 
         </execution> 
        </executions> 
       </plugin> 
      </plugins> 
     </build> 
    </profile> 
    <profile> 
     <id>logReader</id> 
     <build> 
      <finalName>${project.artifactId}</finalName> 
      <plugins> 
       ... 
       <plugin> 
        <artifactId>maven-assembly-plugin</artifactId> 
        <version>2.4</version> 
        <configuration> 
         <appendAssemblyId>false</appendAssemblyId> 
         <finalName>${project.artifactId}-logReader</finalName> 
         <archive> 
          <manifest> 
           <mainClass>fully.qualified.path.LogReader</mainClass> 
          </manifest> 
         </archive> 
         <descriptorRefs> 
          <descriptorRef>jar-with-dependencies</descriptorRef> 
         </descriptorRefs> 
        </configuration> 
        <executions> 
         <execution> 
          <phase>package</phase> 
          <goals> 
           <goal>single</goal> 
          </goals> 
         </execution> 
        </executions> 
       </plugin> 
      </plugins> 
     </build> 
    </profile> 
</profiles> 

真的,两者之间的唯一区别是因为插件中定义的“finalName”和“mainClass”元素。

当我试图在两个配置文件(我使用IntelliJ IDEA,顺便说一句)上执行mvn:package时,我得到两个.jar文件,但一个是正确的,另一个是不正确的。 “正确的”包含所有依赖项和一个有效的MANIFEST.MF文件。在“不正确”一个不包含任何依赖性和MANIFEST.MF文件缺少“主类”属性,我需要为了它是可执行的。

我发现,如果我只运行一个配置文件或其他,它工作正常,但如果我尝试一次执行这两个配置文件,它失败。我也得到我的日志中的下列事项,但我必须承认,我完全不懂他们在说什么:

[INFO] Building jar: .../target/publisher.jar 
... 
[INFO] Building jar: .../target/publisher-logReader.jar 
[WARNING] Configuration options: 'appendAssemblyId' is set to false, and 'classifier' is missing. 
Instead of attaching the assembly file: .../target/publisher-logReader.jar, it will become the file for main project artifact. 
NOTE: If multiple descriptors or descriptor-formats are provided for this project, the value of this file will be non-deterministic! 
[WARNING] Replacing pre-existing project main-artifact file: .../target/publisher.jar with assembly file: .../target/publisher-logReader.jar 

这个有什么想法?是否有可能产生两个jar文件与单个MVN:包这样,还是我找错了树?

谢谢!

回答

30

因此,一旦我贴这个,我发现这个线程:

Create multiple runnable Jars (with depencies included) from a single Maven project

这将使用不同的方法,因为它不使用两个配置文件,它使用两个执行,因为这样的:

<plugin> 
    <artifactId>maven-assembly-plugin</artifactId> 
    <version>2.4</version> 
    <executions> 
     <execution> 
      <id>build-publisher</id> 
      <configuration> 
       <appendAssemblyId>false</appendAssemblyId> 
       <archive> 
        <manifest> 
         <mainClass>fully.qualified.path.Publisher</mainClass> 
        </manifest> 
       </archive> 
       <descriptorRefs> 
        <descriptorRef>jar-with-dependencies</descriptorRef> 
       </descriptorRefs> 
       <finalName>${project.artifactId}</finalName> 
      </configuration> 
      <phase>package</phase> 
      <goals> 
       <goal>single</goal> 
      </goals> 
     </execution> 
     <execution> 
      <id>build-logReader</id> 
      <configuration> 
       <appendAssemblyId>false</appendAssemblyId> 
       <archive> 
        <manifest> 
         <mainClass>fully.qualified.path.LogReader</mainClass> 
        </manifest> 
       </archive> 
       <descriptorRefs> 
        <descriptorRef>jar-with-dependencies</descriptorRef> 
       </descriptorRefs> 
       <finalName>${project.artifactId}-logReader</finalName> 
      </configuration> 
      <phase>package</phase> 
      <goals> 
       <goal>single</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 

这似乎是工作。故事的寓意似乎是,我不完全理解配置文件或何时应该使用它们。

+1

与[缺少''和''元素的[this]](http://stackoverflow.com/a/8726969/288875)相比,此解决方案效果更好(至少对我来说)效果更好。 – 2013-06-25 12:25:29

相关问题