2018-07-15 (IDEA)Maven to build JAR which depends on JAR from another local project

Note

In the practice, we may want to divide the system into modules, some are just library to be referenced by other modules, to help co-work & maintenance of the system.
This guide resolves the question of 'copying the JAR from another local project' under the conditions:

  • I have one a project-A that has common classes to be used among the team
  • Another project-B depends on this common project
  • I want the common project-A to be packaged as a JAR, which can be deployed together project-B
  • The project-B will be packaged as JAR, not WAR

And this project just use the pure Maven, not Spring-Boot.

Step 1 Create the parent project

We define the whole system as a project, which contains other project as its modules. So firstly we need to create the high-level project. This parent-project doesn't need to contain any code.

  • In IDEA, select File --> New --> Project

    mj0001.png

  • In the popped 'New Project' windows, select Maven from the left-panel, do NOT check Create from archetype on the right-panel, then press Next

    mj0002.png

  • Input GroupId, ArtifactId for the project and press Next

    mj0003.png

  • Review the properties of the project and press Finish

    mj0004.png

  • Review the project created


    mj0005.png
  • Right-click on the src folder and select Delete to delete the src folder from the parent-project, since it doesn't need any code

    mj0006.png

    mj0007.png

  • Review the current status of the parent-project


    mj0008.png
  • In the file pom.xml, define the packaging of the parent-project to be pom

    mj0009.png

Step 2 Create the main-entry

  • Right-click on parentproject, select New --> Module

    mj0010.png

  • Select Maven from the left-panel, do NOT check Create from archetype on the right-panel, then press Next

    mj0011.png

  • Input the artifactId and click Next

    mj0012.png

  • Review the values and click Finish

    mj0013.png

  • Check pom.xml of provider, to see that there is a <parent></parent> definition

    mj0014.png

  • Check pom.xml of parent-project, to see that one module has been added automatically

    mj0015.png

  • Right-click on provider --> src, select Mark Directory as --> Sources Root

    mj0016.png

  • Right-click on provider --> src --> main --> java(its icon has been changed now), select New --> Java Class

    mj0017.png

  • Name the class as AppProvider, click OK

    mj0018.png

  • Add a main() method to AppProvider, simply print a message

    mj0019.png

  • Click the run icon to run the main method

    mj0020.png

  • It works now~~~

Step 3 Run the main-entry from command-line

  • Open a command-line window, change directory to the folder of provider

  • Compile & see the result, using mvn clean compile

    mj0021.png

  • Build the package, check the content of the JAR, and run the package from command-line


    mj0022.png

We can add some configuration to the pom.xml to tell it the main-entry-class

  • Open pom.xml of AppProvider, add reference to maven-jar-plugin and specify the class-path & main-Class

    mj0023.png

  • Back to command-line and try again. This time we don't need to specify the main-class again


    mj0024.png

Step 4 Add a library

We suppose to use some library to hold shared interfaces/classes/utilities. It could be referenced by other parts of the project.

  • Right-click on parentproject, select New --> Module
  • Select Maven from the left-panel, do NOT check Create from archetype on the right-panel, then press Next
  • Input the artifactId to be service, and click Next
  • Review the values and click Finish
  • Check pom.xml of service, to see that there is a <parent></parent> definition
  • Check pom.xml of parent-project, to see that one module has been added automatically
  • Right-click on service --> src, select Mark Directory as --> Sources Root
  • Right-click on service --> src --> main --> java(its icon has been changed now), select New --> Java Class
  • Name the class as SampleService, click OK
  • Add a public method to SampleService, simply print another message
    mj0025.png

Step 5 Make provider to depend on service

  • Open AppProvider under module provider

  • In the main-method, type SampleService, press Alt + Enter and select to Add dependency on module 'service'

    mj0026.png

  • Finish the code to invoke the method from SampleService

    mj0027.png

  • Back to command-line, change directory to folder of parent-project, try to mvn install

    mj0028.png

From the command-line we know that even IDEA imported the class for us, Maven doesn't know it. To fix this, open pom.xml of provider, manually add the dependency to module service

mj0029.png

  • Back to command-line window and try again, this time we see the success!


    mj0030.png
  • Check the content under provider\target and try execute the JAR

    mj0031.png

Step 6 Copy the referenced JAR to lib\

From the output of the step-5, we see that we can build the JAR, but failed when trying to run it.
The error says that it cannot find SampleClass.
By checking the content under provider\target, we found that there is not a JAR from the module service.
We already defined a lib/ in the pom.xml of module provider, in step-3, so we will try some way to copy the referenced JARs into the lib folder.

mj0032.png

More

  • We may gain the same result by configuring maven-plug-in correctly in pom.xml
  • We may refer to Spring-Boot for their usage

These are left for future reading...

Reference

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,178评论 0 10
  • 当除夕的烟火 缓缓拢作流星一颗 消碎了灿然的闪灼 当除夕的盛馔 流散了袅袅的炉烟 分不出辛酸咸淡 那么,一切都将簒...
    姑射阅读 1,733评论 1 2
  • 不合理信念:是人内心中不现实的、不合逻辑的、站不住脚的信念。即那些绝对化的、过分概括化的、极端化的思想认识。 心理...
    会武功的小龙女阅读 7,896评论 0 1
  • 旁友,外挂要伐? 被工作耽误的职业玩家? “你知不知道免费玩家和付费玩家的区别?” “不知道。” “免费玩家是商家...
    自制力才是超能力阅读 4,340评论 0 5
  • 外面的树又绿起来了 春天和夏天重叠在一起 仍不及这一抹 我想告诉你的是—— 你就是我眼睛所顾盼的那一幅 秋天的风又...
    花不晚阅读 1,301评论 0 0

友情链接更多精彩内容