IntelliJ IDEA如何打开多个Maven的module且相互调用

小编给大家分享一下IntelliJ IDEA如何打开多个Maven的module且相互调用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

###1、需求

1、IntelliJ IDEA打开多个项目2、每个同学开发一个项目,相互之前独立不影响3、通过一个入口可以调用所有项目类、方法、属性,达到同时开发且检测代码4、dependency只需要写一份,其余项目不用写,便可全部依赖

###2、注意事项(非常重要)

6个坑:

1、<groupId>com.yh.bi</groupId>项目中所有的groupId要一样

2、避免循环依赖,导致程序报错

3、<scope>provided</scope>打包的服务器运行时候需要provided,本机调试的时候,需要注释在一个maven项目中,如果存在编译需要而发布不需要的jar包,可以用scope标签,值设为provided

IntelliJ IDEA如何打开多个Maven的module且相互调用

IntelliJ IDEA如何打开多个Maven的module且相互调用

4、项目、module建好之后需要添加Scala的框架支持

IntelliJ IDEA如何打开多个Maven的module且相互调用

5、在yhProject中,可以统一对所有的module进行清理、编译、打包

IntelliJ IDEA如何打开多个Maven的module且相互调用

6、要运行依赖中的module,则必须要将module中的Jar包,打到maven中,需要使用install

下面,是我将所有module中的Jar打到Maven中的路径:

IntelliJ IDEA如何打开多个Maven的module且相互调用

IntelliJ IDEA如何打开多个Maven的module且相互调用

###3、建立Project和建立module

1、只需要建立一个项目,其他项目由module建立,所有module且放在项目中。2、本文项目为yhproject,其余都为module,分别是:mainEntrance、yhutils、yhapp、yhweb、yhgame

项目建立步鄹:

IntelliJ IDEA如何打开多个Maven的module且相互调用

IntelliJ IDEA如何打开多个Maven的module且相互调用

IntelliJ IDEA如何打开多个Maven的module且相互调用

IntelliJ IDEA如何打开多个Maven的module且相互调用

Module建立步鄹:

IntelliJ IDEA如何打开多个Maven的module且相互调用

IntelliJ IDEA如何打开多个Maven的module且相互调用

IntelliJ IDEA如何打开多个Maven的module且相互调用

项目、所有module、部分module代码展示:

IntelliJ IDEA如何打开多个Maven的module且相互调用

IntelliJ IDEA如何打开多个Maven的module且相互调用

###4、项目之前的依赖关系

IntelliJ IDEA如何打开多个Maven的module且相互调用

###5、代码展示

mainEntrance

package com.yh.bi.dag

packagecom.yh.bi.dag

/**
*Createdbyyuhuion2017/2/10.
*/
importjava.time.{Duration,LocalDate}
importcom.yh.bi._
importcom.yh.bi.{UserAPP,UserGame,UserWEB}
importorg.slf4j.LoggerFactory

importscala.collection.immutable.{ListMap,ListSet}

/**
*Createdbyyuhuion2016/8/25.
*task-->Node-->DAG-->DAGExecutor
*/

caseclassNode[T](task:T,parent:T*){
overridedeftoString:String={
s"$task(${parent.mkString(",")})"
}
}

caseclassDAG[T](nodes:Node[T]*)

caseclassDAGExecutor[T](dag:DAG[T]){
privatevalLOG=LoggerFactory.getLogger(this.getClass)
privateval_nodes:Map[T,Seq[T]]=dag.nodes.map(node=>(node.task,node.parent.filter(_!=null))).toMap
privatevar_pending:Set[T]=ListSet()
privatevar_fails=ListMap[T,String]()
privatevar_success=Seq[T]()

//判断Node的task节点的父节点运行状态(flase,true)
privatedefgetPending:Option[T]={
_pending.find{name=>
valparents=_nodes(name)
!parents.exists(name=>!_success.contains(name))
}
}

privatedeffail(name:T,message:String):Unit={
_pending-=name
_fails+=name->message
for(child<-_pending.filter(child=>_nodes(child).contains(name))){
fail(child,s"依赖的任务无法执行:$name")
}
}

privatedefsuccess(name:T):Unit={
_pending-=name
_success=_success:+name
}

defexecute(func:T=>Unit):Unit={
_pending=_nodes.keySet
_fails=ListMap()
_success=Seq()
varrunning=true

while(running){
valtaskOpt=getPending
if(taskOpt.nonEmpty){
valtask=taskOpt.get
valstartMills=System.currentTimeMillis()
LOG.info("starttask{}",task)
try{
println("=============")
func(task)//执行executor方法
println("+++++++++++++")
valtime=Duration.ofMillis(System.currentTimeMillis()-startMills)
LOG.info(s"endtask$tasktime=$time")
success(task)
}catch{
casee:Throwable=>fail(task,e.getMessage)
LOG.error(e.getMessage,e)
LOG.info(s"failtask$task")
}
}else{
running=false
}
}

for(name<-_success){
LOG.info(s"successtask:$name")
}
for(name<-_fails){
LOG.info(s"failtask:${name._1}-${name._2}")
}
}
}

objectDAG{
valallSDKDAG=newDAG[Task](
Node(UserAPP),
Node(UserGame),
Node(UserWEB)
)

defmain(args:Array[String]):Unit={
DAGExecutor(allSDKDAG).execute{task=>task.executor("appkey":String,LocalDate.now(),LocalDate.now())}
}
}

yhutils

packagecom.yh.bi

/**
*Createdbyyuhuion2017/2/10.
*/
importjava.time.LocalDate
importorg.apache.spark.sql.SQLContext
importorg.slf4j.LoggerFactory

abstractclassExecutorextendsTaskwithSQLContextAware{

overridedefrun(appkey:String,startDay:LocalDate,endDay:LocalDate)={}

}

traitSQLContextAware{
implicitvarctx:SQLContext=_
}


abstractclassTask{

protectedvalLOG=LoggerFactory.getLogger(this.getClass)

defexecutor(appkey:String,startDay:LocalDate,endDay:LocalDate):Unit

defrun(appkey:String,startDay:LocalDate,endDay:LocalDate):Unit={
executor(appkey,startDay,endDay)
}

}

yhapp

packagecom.yh.bi

/**
*Createdbyyuhuion2017/2/10.
*/
importjava.time.LocalDate

objectUserAPPextendsExecutor{

overridedefexecutor(appkey:String,startDay:LocalDate,endDay:LocalDate):Unit={

println("++++我的UserAPP的执行过程++++")

}

}

yhweb

packagecom.yh.bi

importjava.time.LocalDate

objectUserWEBextendsExecutor{

overridedefexecutor(appkey:String,startDay:LocalDate,endDay:LocalDate):Unit={

println("++++我的UserWEB的执行过程++++")

}

}

yhgame

packagecom.yh.bi

/**
*Createdbyyuhuion2017/2/10.
*/
importjava.time.LocalDate

objectUserGameextendsExecutor{

overridedefexecutor(appkey:String,startDay:LocalDate,endDay:LocalDate):Unit={

println("++++我的UserGame的执行过程++++")

}

}

###6、项目中POM依赖展示

yhproject中POM文件展示:

<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.yh.bi</groupId>
<artifactId>yhproject</artifactId>
<packaging>pom</packaging>
<version>1.0</version>

<modules>
<module>mainEntrance</module>
<module>yhapp</module>
<module>yhweb</module>
<module>yhgame</module>
<module>yhutils</module>
</modules>
</project>

mainEntrance中POM文件展示:

<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>mainEntrance</artifactId>
<groupId>com.yh.bi</groupId>
<version>1.0</version>

<dependencies>
<dependency>
<artifactId>yhutils</artifactId>
<groupId>com.yh.bi</groupId>
<version>1.0</version>
<!--<scope>provided</scope>//本机调试则注释,集群运行则解开-->
</dependency>

<dependency>
<artifactId>yhapp</artifactId>
<groupId>com.yh.bi</groupId>
<version>1.0</version>
<!--<scope>provided</scope>-->
</dependency>

<dependency>
<artifactId>yhgame</artifactId>
<groupId>com.yh.bi</groupId>
<version>1.0</version>
<!--<scope>provided</scope>-->
</dependency>

<dependency>
<artifactId>yhweb</artifactId>
<groupId>com.yh.bi</groupId>
<version>1.0</version>
<!--<scope>provided</scope>-->
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4</version>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>**/log4j2.*</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<!--putyourconfigurationshere-->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>${project.build.directory}/${project.artifactId}.jar
</outputFile>
</configuration>
</execution>
</executions>
</plugin>

</plugins>

<sourceDirectory>src/main/scala</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>

</build>


</project>

yhutils中POM文件展示:

<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>yhproject</artifactId>
<groupId>com.yh.bi</groupId>
<version>1.0</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>yhutils</artifactId>

<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-hive_2.11</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4</version>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>**/log4j2.*</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<!--putyourconfigurationshere-->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>${project.build.directory}/${project.artifactId}.jar
</outputFile>
</configuration>
</execution>
</executions>
</plugin>

</plugins>

<sourceDirectory>src/main/scala</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>

</build>
</project>

yhapp中POM文件展示:

<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.yh.bi</groupId>
<artifactId>yhapp</artifactId>
<version>1.0</version>

<dependencies>
<dependency>
<artifactId>yhutils</artifactId>
<groupId>com.yh.bi</groupId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4</version>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>**/log4j2.*</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<!--putyourconfigurationshere-->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>${project.build.directory}/${project.artifactId}.jar
</outputFile>
</configuration>
</execution>
</executions>
</plugin>

</plugins>

<sourceDirectory>src/main/scala</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>

</build>

</project>

yhweb中POM文件展示:

<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.yh.bi</groupId>
<artifactId>yhweb</artifactId>
<version>1.0</version>

<dependencies>
<dependency>
<artifactId>yhutils</artifactId>
<groupId>com.yh.bi</groupId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4</version>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>**/log4j2.*</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<!--putyourconfigurationshere-->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>${project.build.directory}/${project.artifactId}.jar
</outputFile>
</configuration>
</execution>
</executions>
</plugin>

</plugins>

<sourceDirectory>src/main/scala</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>

</build>

</project>

yhgame中POM文件展示:

<?xmlversion="1.0"encoding="UTF-8"?>
<projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.yh.bi</groupId>
<artifactId>yhgame</artifactId>
<version>1.0</version>

<dependencies>
<dependency>
<artifactId>yhutils</artifactId>
<groupId>com.yh.bi</groupId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4</version>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>**/log4j2.*</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<!--putyourconfigurationshere-->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>${project.build.directory}/${project.artifactId}.jar
</outputFile>
</configuration>
</execution>
</executions>
</plugin>

</plugins>

<sourceDirectory>src/main/scala</sourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>

</build>

</project>

###7、运行结果展示

注意:我在mainEntrance执行DAG中的main文件,可以调用且执行了yhutils、yhapp、yhweb、yhgame中的代码

IntelliJ IDEA如何打开多个Maven的module且相互调用

###8、如果建立Java 的module

IntelliJ IDEA如何打开多个Maven的module且相互调用

以上是“IntelliJ IDEA如何打开多个Maven的module且相互调用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注恰卡编程网行业资讯频道!

发布于 2021-07-24 22:37:13
收藏
分享
海报
0 条评论
175
上一篇:IntelliJ IDEA像Eclipse一样打开多个项目的示例分析 下一篇:css中clear属性的使用方法
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码