解决Maven项目相互依赖/循环依赖/双向依赖的问题
很?多?时?候?随?着?项?目?的?膨?胀?,模?块?会?越?来?越?多?,如?果?设?计?上? 稍?有?不?慎?就?会?出?现?模?块?之?间?相?互?依?赖?的?情?况?。?这?对?于?使?用?Maven的?用?户?是?比?较?痛?苦?的?,因?为?出?现?模?块?之?间?相?互?依?赖?的?话?在?构?建?的?时?候?就?会?失?败?,Maven通?常?要?先?编?译?被?依?赖?的?模?块?,如?果?出?现?相?互?依?赖?Maven就?不?知?道?该?怎?么?办?了?。?下?图?描?述?了?三?个?Maven模?块?相?互?依?赖?的?场?景?:
图 1. A、?B、?C三?个?模?块?相?互?依?赖?
图?中?模?块?C依?赖?于?模?块?B,模?块?B依?赖?于?模?块?A,而?模?块?A又?依?赖?于?模?块?C,这?样?就?出?现?了?相?互?依?赖?情?况?,如?果?运?行?mvn compile会?出?现?如?下?错?误?:
[INFO] Scanning for projects... [ERROR] The projects in the reactor contain a cyclic reference: Edge between 'Ve rtex{label='org.kuuyee.sample:module-C:1.0-SNAPSHOT'}' and 'Vertex{label='org.ku uyee.sample:module-B:1.0-SNAPSHOT'}' introduces to cycle in the graph org.kuuyee .sample:module-B:1.0-SNAPSHOT --> org.kuuyee.sample:module-A:1.0-SNAPSHOT --> or g.kuuyee.sample:module-C:1.0-SNAPSHOT --> org.kuuyee.sample:module-B:1.0-SNAPSHO T -> [Help 1][ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit ch.[ERROR] Re-run Maven using the -X switch to enable full debug logging.[ERROR] [ERROR] For more information about the errors and possible solutions, please rea d the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectCycleEx ception
1. 使?用?build-helper-maven-plugin解?决?相?互?依?赖?的?问?题?我?的?解?决?办?法?就?是?先?把?相?互?依?赖?的?模?块?整?合?在?一?起?,相?当?于?把?这?些?模?块?合?并?成?一?个?单?独?的?模?块?统?一?编?译?,
如?下?图?:图 2. 合?并?A、?B、?C三?个?模?块?为?D模?块?
这?样?就?产?生?了?一?个?合?并?模?块?D,我?们?把?它?当?做?一?个?辅?助?构?建?模?块?,然?后?让?A、?B、?C模?块?都?依?赖?于?D模?块?,这?样?的?话?就?可?以?成?功?编?译?A、?B和?C模?块?,
如?下?图?: 图 3. 基?于?D模?块?来?分?别?编?译?A、?B、?C三?个?模?块?
要?想?把?A、?B、?C三?个?模?块?整?合?在?一?起?编?译?,需?要?借?助?build-helper-maven-plugin插?件?,这?个?插?件?在?Maven构?建?周?期?提?供?一?些?辅?助?功?能?,下?面?列?出?插?件?的?提?供?的?功?能?列?表?: build-helper:add-source:添?加?更?多?的?构?建?源?码?目?录? build-helper:add-test-source:添?加?更?多?的?测?试?源?码?目?录? build-helper:add-resource:添?加?更?多?的?资?源?目?录? build-helper:add-test-resource:添?加?更?多?的?测?试?资?源?目?录? build-helper:attach-artifact:在?安?装?和?部?署?周?期?附?加?artifacts build-helper:maven-version:添?加?一?个?指?定?当?前?Maven版?本?的?属?性? build-helper:parse-version:添?加?一?个?指?定?组?件?版?本?的?属?性? build-helper:released-version:决?定?当?前?项?目?的?最?终?版?本? build-helper:remove-project-artifact:从?本?地?资?源?库?中?移?除?项?目?的?artifacts build-helper:reserve-network-port:Reserve a list of random and unused network ports. 在?这?里?我?们?要?用?到?build-helper:add-source这?个?功?能?,将?模?块?A、?B、?C的?源?码?路?径?加?进?来?。? 我?们?再?添?加?一?个?辅?助?模?块?D,在?辅?助?模?块?D中?使?用?build-helper-maven-plugin插?件?,然?后?让?模?块?A、?B、?C都?依?赖?于?辅?助?模?块?D,模?块?D的?POM模?型?如?下?: 例 1. 辅?助?模?块?D的?POM模?型?
<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"><parent><groupId>org.kuuyee.sample</groupId><artifactId>sample-parent</artifactId><version>1.0-SNAPSHOT</version><relativePath>../../pom.xml</relativePath></parent><modelVersion>4.0.0</modelVersion><groupId>org.kuuyee.sample</groupId><artifactId>module-D</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><name>module-D</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><module.a.src>../../module/module-A/src/main/java</module.a.src><module.b.src>../../module/module-B/src/main/java</module.b.src><module.c.src>../../module/module-C/src/main/java</module.c.src></properties><build><plugins><!-- 解决模块相互依赖,综合所有相互依赖代码统一编译 --><plugin><groupId>org.codehaus.mojo</groupId><artifactId>build-helper-maven-plugin</artifactId><executions><execution><id>add-source</id><phase>generate-sources</phase><goals><goal>add-source</goal></goals><configuration><sources><source>${module.a.src}</source><source>${module.b.src}</source><source>${module.c.src}</source></sources></configuration></execution></executions></plugin></plugins></build><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies></project>