IntelliJ IDEA打开多个Maven的module且相互调用代码的方法

2025-05-29 0 63

###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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102
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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31
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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
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

?

1

2

3

4

5

6

7

8

9

10

11

12

13
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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
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文件展示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19
<?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文件展示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97
<?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文件展示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77
<?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文件展示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75
<?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文件展示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75
<?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文件展示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75
<?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中的代码

IntelliJ IDEA打开多个Maven的module且相互调用代码的方法

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

IntelliJ IDEA打开多个Maven的module且相互调用代码的方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:https://blog.csdn.net/silentwolfyh/article/details/54970108?utm_source=blogxgwz2

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 IntelliJ IDEA打开多个Maven的module且相互调用代码的方法 https://www.kuaiidc.com/109684.html

相关文章

发表评论
暂无评论