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
4、项目、module建好之后需要添加scala的框架支持
5、在yhproject中,可以统一对所有的module进行清理、编译、打包
6、要运行依赖中的module,则必须要将module中的jar包,打到maven中,需要使用install
下面,是我将所有module中的jar打到maven中的路径:
###3、建立project和建立module
1、只需要建立一个项目,其他项目由module建立,所有module且放在项目中。
2、本文项目为yhproject,其余都为module,分别是:mainentrance、yhutils、yhapp、yhweb、yhgame
项目建立步鄹:
module建立步鄹:
项目、所有module、部分module代码展示:
###4、项目之前的依赖关系
###5、代码展示
mainentrance
package com.yh.bi.dag
package com.yh.bi.dag /** * created by yuhui on 2017/2/10. */ import java.time.{duration, localdate} import com.yh.bi._ import com.yh.bi.{userapp, usergame, userweb} import org.slf4j.loggerfactory import scala.collection.immutable.{listmap, listset} /** * created by yuhui on 2016/8/25. * task --> node --> dag --> dagexecutor */ case class node[t](task: t, parent: t*) { override def tostring: string = { s"$task(${parent.mkstring(",")})" } } case class dag[t](nodes: node[t]*) case class dagexecutor[t](dag: dag[t]) { private val log = loggerfactory.getlogger(this.getclass) private val _nodes: map[t, seq[t]] = dag.nodes.map(node => (node.task, node.parent.filter(_ != null))).tomap private var _pending: set[t] = listset() private var _fails = listmap[t, string]() private var _success = seq[t]() //判断node的task节点的父节点运行状态(flase ,true) private def getpending: option[t] = { _pending.find { name => val parents = _nodes(name) !parents.exists(name => !_success.contains(name)) } } private def fail(name: t, message: string): unit = { _pending -= name _fails += name -> message for (child <- _pending.filter(child => _nodes(child).contains(name))) { fail(child, s"依赖的任务无法执行: $name") } } private def success(name: t): unit = { _pending -= name _success = _success :+ name } def execute(func: t => unit): unit = { _pending = _nodes.keyset _fails = listmap() _success = seq() var running = true while (running) { val taskopt = getpending if (taskopt.nonempty) { val task = taskopt.get val startmills = system.currenttimemillis() log.info("start task {}", task) try { println("=============") func(task) //执行executor方法 println("+++++++++++++") val time = duration.ofmillis(system.currenttimemillis() - startmills) log.info(s"end task $task time=$time") success(task) } catch { case e: throwable => fail(task, e.getmessage) log.error(e.getmessage, e) log.info(s"fail task $task") } } else { running = false } } for (name <- _success) { log.info(s"success task: $name") } for (name <- _fails) { log.info(s"fail task: ${name._1} - ${name._2}") } } } object dag { val allsdkdag = new dag[task]( node(userapp), node(usergame), node(userweb) ) def main(args: array[string]): unit = { dagexecutor(allsdkdag).execute { task =>task.executor("appkey": string, localdate.now(), localdate.now())} } }
yhutils
package com.yh.bi /** * created by yuhui on 2017/2/10. */ import java.time.localdate import org.apache.spark.sql.sqlcontext import org.slf4j.loggerfactory abstract class executor extends task with sqlcontextaware { override def run(appkey: string, startday: localdate, endday: localdate)={} } trait sqlcontextaware { implicit var ctx: sqlcontext = _ } abstract class task { protected val log = loggerfactory.getlogger(this.getclass) def executor(appkey: string, startday: localdate, endday: localdate): unit def run(appkey: string, startday: localdate, endday: localdate): unit = { executor(appkey, startday, endday) } }
yhapp
package com.yh.bi /** * created by yuhui on 2017/2/10. */ import java.time.localdate object userapp extends executor{ override def executor(appkey: string, startday: localdate, endday: localdate): unit = { println("++++我的userapp的执行过程++++") } }
yhweb
package com.yh.bi import java.time.localdate object userweb extends executor{ override def executor(appkey: string, startday: localdate, endday: localdate): unit = { println("++++我的userweb的执行过程++++") } }
yhgame
package com.yh.bi /** * created by yuhui on 2017/2/10. */ import java.time.localdate object usergame extends executor{ override def executor(appkey: string, startday: localdate, endday: localdate): unit = { println("++++我的usergame的执行过程++++") } }
###6、项目中pom依赖展示
yhproject中pom文件展示:
<?xml version="1.0" encoding="utf-8"?> <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>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文件展示:
<?xml version="1.0" encoding="utf-8"?> <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> <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> <!-- put your configurations here --> </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文件展示:
<?xml version="1.0" encoding="utf-8"?> <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> <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> <!-- put your configurations here --> </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文件展示:
<?xml version="1.0" encoding="utf-8"?> <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>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> <!-- put your configurations here --> </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文件展示:
<?xml version="1.0" encoding="utf-8"?> <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>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> <!-- put your configurations here --> </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文件展示:
<?xml version="1.0" encoding="utf-8"?> <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>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> <!-- put your configurations here --> </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中的代码
###8、如果建立java 的module
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。