美文网首页
2018-07-15 (IDEA)Maven to build

2018-07-15 (IDEA)Maven to build

作者: 猪迹 | 来源:发表于2018-07-16 01:39 被阅读0次

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

相关文章

网友评论

      本文标题:2018-07-15 (IDEA)Maven to build

      本文链接:https://www.haomeiwen.com/subject/uffmpftx.html