最近项目涉及到了多源码目录的问题,因为是用的maven管理项目,虽然maven可以配置源码目录,但是不支持多目录结构。如果要实现多源码目录,需要应用相应插件。
下面多maven的源码目录的配置进行简易分析。图1为项目结构。IDE使用的是eclipse。
新增forg的源码目录和prop的配置目录。默认配置的jar包
(jar包中没有 forg目录和prop.properties文件)
maven配置指定源码目录,增加资源目录
maven有一套默认的目录结构,使用maven一般需要遵循此结构来组织项目,默认的配置结构说明文档
http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
maven可以在pom文件中修改默认的配置。官方的QA如下:
http://maven.apache.org/general.html#dir-struct
文中提及
By configuring <sourceDirectory>, <resources> and other elements of the <build> section
修改pom方法例子
1、修改默认的源码目录和修改资源文件
<sourceDirectory>${project.basedir}/src/forg</sourceDirectory>
<resources>
<resource>
<directory>
${project.basedir}/src/prop
</directory>
<includes>
<include>
**/*.properties
</include>
</includes>
</resource>
<resource>
<directory>
${project.basedir}/src/main/resources
</directory>
</resource>
</resources>
修改后clean install的jar包
2.多源码目录配置
maven不支持多源码的配置,需要引用第三方插件Build Helper Maven Plugin 。当前此插件的版本是1.9.1.
配置方法:
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/forg</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
配置后的jar包成功打入两个目录下的源码文件。
3.eclipse的m2e报错
maven打包是用的命令行。eclipse的m2e插件不支持上述配置,有报错。官方说明:
http://eclipse.org/m2e/documentation/m2e-execution-not-covered.html
官方也提供了解决方案,我选择了最后一种方案(配置lifecycleMappingMetadata):
<?xml version="1.0" encoding="UTF-8"?>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<goals>
<goal>add-source</goal>
</goals>
<versionRange>[0.0,)</versionRange>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
reload配置之后错误消失。
附上maven官方文档地址(大多问题都能找到答案)
http://maven.apache.org/guides/index.html