Maven配置多源代码目录、多资源目录

 

在本地机器搭建一个开源项目的源码环境时,把其转换为maven项目,因模块业务划分,不同的业务放在不同的源目录下,这样转换出来的maven项目结构如下:

pom.xml内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>sokeeper</groupId>
  <artifactId>sokeeper</artifactId>
  <version>3.4.6</version>
  <name>sokeeper</name>
  <description>sokeeper</description>

  <properties>
    <target.dir>target</target.dir>
  </properties>

  <dependencies>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.16</version>
    </dependency>
    <dependency>
           .....
    </dependency>
  </dependencies>
</project>

 

在使用过程中发现,在eclipse中可以正常使用,但使用mvn compile命令进行编译的时候,target/classes目录下始终没有编译后的*.class文件。查看maven执行日志发现有一行日志如下:

Nothing to compile all classes are up to date

后经验证发现,所有在src/main/java目录下的代码,都会被编译成功。而在src/java/main、src/java/generated源目录下的代码不会被编译。这时,才忽然想起maven项目的默认项目结构是这样的:

然后,才想明白为啥出现eclipse中可以编译,使用mvn compile不能正常编译。原来,在创建maven项目后,我把代码拷贝到了src/java/main目录下,并把src/java/main目录作为了源代码目录(Source Code Folder),这样eclipse的自动编译项目功能,就会自动编译所有源代码目录下的java文件(同理src/java/generated)。而使用maven命令时,maven项目默认以src/main/java为源代码目录,会把src/main/java这个目录下的所有java文件进行编译,其它的源代码目录下的java文件,则不会被maven编译。

既然已经知道问题出现在这里,就可以寻思解决方案了。要么,可以直接把src/java/main、src/java/generated目录下的java代码都移到maven默认的源代码目录src/main/java下;要么,让maven在执行编译命令的时候,也去编译src/java/main、src/java/generated目录的java文件。

从网上搜索资料,发现maven的默认源代码、资源文件、测试源代码目录配置可以修改配置:

<build>
    <!--默认源代码目录-->
    <sourceDirectory>src/main/java </sourceDirectory>  
    <!--默认测试源代码目录-->
    <testSourceDirectory>src/test/java</testSourceDirectory>  
    <!--默认资源目录-->
    <resources>  
        <resource>  
            <directory>src/main/resources</directory>  
        </resource>  
    </resources> 
    <!--默认测试资源目录--> 
    <testResources>  
        <testResource>  
             <directory>src/test/resources</directory>  
        </testResource>  
    </testResources>  
</build>

 

但<sourceDirectory>只能指定一个源代码目录,不能指定多个,继续查找,又发现了一个插件build-helper-maven-plugin。发现这个插件可以指定多个源代码目录、多个资源目录,这个插件就可以实现我的需求。用法如下:

<plugins>  
        <!-- 指定多个源代码目录、多个资源文件目录 -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.8</version>
        <executions>
          <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>src/java/main</source>
                <source>src/java/generated</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin> 
    </plugins>  
</build>

 

配置好build-helper-maven-plugin插件后,在eclipse中右键项目,maven->update project configuration。

这样当前项目中的src/java/main、src/java/generated不再单单是eclipse认识的源代码目录,还是maven的源代码目录。maven再执行编译时,就会到src/java/main、src/java/generated、src/main/java目录下去编译java文件。如此,便解决了问题。

 

另介绍几种maven插件的配置<build>    <plugins>

      <!-- 指定多个源代码目录、多个资源文件目录 -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.8</version>
        <executions>
          <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>src/java/main</source>
                <source>src/java/generated</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <!-- 编译插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
           <encoding>UTF8</encoding> 
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <!-- 生成源文件jar包文件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>2.1.1</version>
        <executions>
          <execution>
            <id>attach-sources</id>
            <goals>
              <goal>jar-no-fork</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <!-- 打字节码包插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <includes>
            <classesDirectory>org/</classesDirectory>
          </includes>
          <executions>
            <execution>
              <id>make-a-jar</id>
              <phase>compile</phase>
              <goals>
                <goal>jar</goal>
              </goals>
            </execution>
          </executions>
        </configuration>
      </plugin>
      <!-- 部署插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <descriptors>
            <descriptor>assembly.xml</descriptor>
          </descriptors>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins> 
</build>

 



posted @ 2020-05-26 11:09  彬在俊  阅读(2787)  评论(0编辑  收藏  举报