在本地機器搭建一個開源項目的源碼環境時,把其轉換為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>