2013-07-05 47 views
20

有什么办法在Maven中的父项目的模块之间共享资源?例如,我想为多模块Maven项目中的所有模块指定一个log4j.properties文件。在多模块maven项目中指定公共资源

通常,我使用Eclipse IDE通过选择常规项目来创建父项目,然后通过指定pom的包装将其转换为Maven项目。这创建了一个“干净”的项目结构,没有src等文件夹。我想知道这种共享资源应该放在哪里。

EDIT1:我想把公共资源放在父项目中。

回答

21

我会创建一个额外的“基本”模块(项目),打包“jar”,其中包含公共资源src/main/resources。然后我会让其他模块依赖于该项目。现在他们在他们的类路径中看到了公共资源。

+1

是,这似乎是一个可能的解决方案。但我有兴趣是否可以在父项目中指定这些资源(不需要引入额外的模块),因为父项目指定了子模块的所有常见依赖项和Maven配置,我认为父项目是最合适的地方也用于共同资源。 – jilt3d

+4

“pom”包装的父项目没有任何资源,它只是一个构建文件,可以作为工件进行安装/部署。 –

+0

我明白了。所以用Maven父项目做这件事是不可能的? +1,但如果还有其他建议,我将暂时解决问题。 – jilt3d

2

Antoher的可能性是使用远程资源包。你可以在父项目中配置它。在这个例子中,我想复制一些文件来测试。如果你使用这个,你将需要在另一个项目中创建这个包。

<plugin>  
     <groupId>org.apache.maven.plugins</groupId>   
     <artifactId>maven-remote-resources-plugin</artifactId> 
     <version>1.5</version> 
     <configuration> 
      <resourceBundles> 
       <resourceBundle>es.sca:myBundle:1.0.0</resourceBundle> 
      </resourceBundles> 
      <attachToMain>false</attachToMain> 
      <attachToTest>true</attachToTest> 
      <appendedResourcesDirectory>${basedir}/src/test/resources</appendedResourcesDirectory> 
     </configuration>    
     <executions> 
      <execution> 
       <goals> 
        <goal>process</goal> 
       </goals> 
      </execution> 
     </executions>     
    </plugin>     
+0

你能分享“你如何访问共享资源”的代码片段吗? –

-1

我设法它像这样工作:

我创建一个项目/封装/测试/资源/ META-INF/persistence.xml文件,并加入到我的pom.xml:

<plugin> 
 
    <groupId>org.apache.maven.plugins</groupId> 
 
    <artifactId>maven-resources-plugin</artifactId> 
 
    <executions> 
 
     <execution> 
 
      <id>copy-test-persistence-xml-resources</id> 
 
      <phase>process-test-sources</phase> 
 
      <goals> 
 
       <goal>copy-resources</goal> 
 
      </goals> 
 
      <configuration> 
 
       <outputDirectory>src/</outputDirectory> 
 
       <resources> 
 
        <resource> 
 
         <directory>${project.parent.basedir}/assembly/</directory> 
 
         <filtering>true</filtering> 
 
        </resource> 
 
       </resources> 
 
      </configuration> 
 
     </execution> 
 
    </executions> 
 
</plugin>

+1

这不会工作,是非常糟糕的做法。 – Anony

0

是,它似乎一个可能的解决方案。但我感兴趣的是, 是否可以在父项目中指定这些资源(不包括 引入附加模块),因为父项目指定所有 共同的依赖关系和子模块 的Maven配置,我认为父项目是最适合的地方 也用于共同资源。

在包装类型POM,当目标规定来管理您的共享资源,只需添加下一个(查看文件夹)到构建POM文件的部分的情况下:

 <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-resources-plugin</artifactId> 
     <executions> 
      <execution> 
       <id>copy-config-files</id> 
       <phase>package</phase> 
       <goals> 
        <goal>copy-resources</goal> 
       </goals> 
       <configuration> 
        <outputDirectory>${project.build.directory}/logconfig</outputDirectory> 
        <resources> 
         <resource> 
         <filtering>false</filtering> 
         <directory>${project.basedir}/src/main/resources</directory> 
         </resource> 
        </resources> 
       </configuration> 
     </execution> 
     </executions> 
    </plugin>