2012-02-24 145 views
2

我想用Maven打包一個Web應用程序作爲可運行的JAR文件。 jar -with-dependencies assembly照顧包括所有依賴項,但我仍然需要包含src/main/webapp如何使用Maven將目錄添加到JAR文件中?

我設法目錄使用自定義組件添加到我的JAR:

<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>webapp</id> 
    <formats> 
     <format>jar</format> 
    </formats> 
    <includeBaseDirectory>false</includeBaseDirectory> 
    <fileSets> 
     <fileSet> 
      <directory>src/main/webapp</directory> 
      <outputDirectory>/webapp</outputDirectory> 
     </fileSet> 
    </fileSets> 
</assembly> 

而且它的工作,它甚至可以一起使用該組件JAR-與依賴性。但是,最終的JAR包含webapp目錄和所有依賴項,但不包含我的項目的類文件。我的大會顯然是刪除它們。

我可以在程序集中保留我自己項目的類文件嗎?或者有另一種方法將目錄添加到JAR中?

回答

5

我的組件顯然刪除它們

我猜這是周圍的其他方法,它不加他們。

您的類文件位於目標/類中,它們需要進入target/webapp/WEB-INF/classes。我猜你需要像這樣的另一個規則:

<fileSet> 
    <directory>target/classes</directory> 
    <outputDirectory>/webapp/WEB-INF/classes</outputDirectory> 
</fileSet> 
1

您將在$ {project.build.directory}/classes(可能是/ target/classes)中生成您的類。

因此,您應該使用該文件夾作爲源目錄。

嘗試改變<directory>src/main/webapp</directory><directory>{project.build.directory}/classes</directory>

0

或者,您可以在您的程序集描述符中添加以下內容。這不僅要考慮放置類文件,還要考慮web應用程序的內容,假設你的maven項目正在構建一個web應用程序。

... 
<dependencySets> 
    <dependencySet> 
     <useProjectArtifact>true</useProjectArtifact> 
    </dependencySet> 
</dependencySets> 
... 
相關問題