2014-01-14 47 views
0

如何讓Eclipse識別出我使用maven-dependency-plugin並執行之前通過WTP向Tomcat部署資源?使Eclipse尊重Maven-Dependency-Plugin

a previous question我配置了Maven以將一些工件複製到war應用程序中,因此我可以將它們提供給Web客戶端。將它們複製到target/${project.artifactId}-${project.version}的方法在通過Maven命令行進行打包時起作用。不幸的是,使用Eclipse的Tomcat集成時沒有這樣的運氣。

Maven插件配置:

<plugin> 
<groupId>org.apache.maven.plugins</groupId> 
<artifactId>maven-dependency-plugin</artifactId> 
<version>2.8</version> 
<executions> 
    <execution> 
     <id>copy</id> 
     <phase>compile</phase> 
     <goals> 
      <goal>copy</goal> 
     </goals> 
     <configuration> 
      <artifactItems> 
       <artifactItem> 
        <groupId>junit</groupId> 
        <artifactId>junit</artifactId> 
        <version>3.8.1</version> 
        <type>jar</type> 
        <overWrite>false</overWrite> 
        <destFileName>optional-new-name.jar</destFileName> 
       </artifactItem> 
      </artifactItems> 
      <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/</outputDirectory> 
      <overWriteReleases>false</overWriteReleases> 
      <overWriteSnapshots>true</overWriteSnapshots> 
     </configuration> 
    </execution> 
</executions> 

回答

1

這個答案假定您使用Eclipse的Java EE開普勒SR1(或近期),來與Maven的集成插件(M2E和M2E-WTP)。

m2e-wtp,用於WTP插件的Maven Integration忽略Maven CLI構建並配置WTP從已知位置發佈資源。

所以,你需要做兩件事情:

  • 配置的依賴插件日食期間罐子複製到一個已知的M2E-WTP位置建立
  • 告訴M2E實際運行過程中的項目配置更新的依賴插件。

首先,定義一個新的Maven財產,你的屬性部分:

<jars.directory>${project.build.directory}/${project.artifactId}-${project.version}/</jars.directory> 

在你的Maven的依賴,插件配置,使用:

<outputDirectory>${jars.directory}</outputDirectory> 

現在應該給你相同使用Maven CLI構建結果。然後,你需要使用特定的M2E配置文件,即會自動在Eclipse中的所有其他情況下運行,並忽略時啓用:

<profiles> 
    <profile> 
     <id>m2e</id> 
     <!-- This profile is only activated when building in Eclipse with m2e --> 
     <activation> 
      <property> 
       <name>m2e.version</name> 
      </property> 
     </activation> 
     <properties> 
      <jars.directory>${project.build.directory}/m2e-wtp/web-resources/</jars.directory> 
     </properties> 
    </profile> 
</profiles> 

最後,我們需要讓M2E知道這是確定運行Maven的dependency-插件:在項目配置期間複製。下面的代碼片段添加到您的pluginManagement部分:

<pluginManagement> 
    <plugins> 
     <!--This plugin's configuration is used to store Eclipse m2e settings 
      only. It has no influence on the Maven build itself. --> 
     <plugin> 
      <groupId>org.eclipse.m2e</groupId> 
      <artifactId>lifecycle-mapping</artifactId> 
      <version>1.0.0</version> 
      <configuration> 
       <lifecycleMappingMetadata> 
        <pluginExecutions> 
         <pluginExecution> 
          <pluginExecutionFilter> 
           <groupId>org.apache.maven.plugins</groupId> 
           <artifactId>maven-dependency-plugin</artifactId> 
           <versionRange>[2.8,)</versionRange> 
           <goals> 
            <goal>copy</goal> 
           </goals> 
          </pluginExecutionFilter> 
          <action> 
           <execute> 
            <runOnConfiguration>true</runOnConfiguration> 
            <runOnIncremental>false</runOnIncremental> 
           </execute> 
          </action> 
         </pluginExecution> 
        </pluginExecutions> 
       </lifecycleMappingMetadata> 
      </configuration> 
     </plugin> 
    </plugins> 
</pluginManagement> 

現在,確保M2E知道所有這些配置更改:按下Alt + F5調出更新的Maven配置對話框,然後單擊確定。構建完成後,您應該在「項目資源管理器」視圖中的Deployed Resources> web-resources下看到您的jar。

從現在起,到Tomcat的部署應該在webapp的根目錄中包含optional-new-name.jar。

+0

謝謝,這是詳細的,看起來很有希望。當我有機會嘗試時,我會告訴你! –