2018-07-15 (IDEA)Maven to build
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 checkCreate from archetype
on the right-panel, then pressNext
mj0002.png
-
Input
GroupId
,ArtifactId
for the project and pressNext
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 selectDelete
to delete thesrc
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 bepom
mj0009.png
Step 2 Create the main-entry
-
Right-click on
parentproject
, selectNew --> Module
mj0010.png
-
Select
Maven
from the left-panel, do NOT checkCreate from archetype
on the right-panel, then pressNext
mj0011.png
-
Input the artifactId and click
Next
mj0012.png
-
Review the values and click
Finish
mj0013.png
-
Check
pom.xml
ofprovider
, to see that there is a<parent></parent>
definition
mj0014.png
-
Check
pom.xml
ofparent-project
, to see that onemodule
has been added automatically
mj0015.png
-
Right-click on
provider --> src
, selectMark Directory as --> Sources Root
mj0016.png
-
Right-click on
provider --> src --> main --> java
(its icon has been changed now), selectNew --> Java Class
mj0017.png
-
Name the class as
AppProvider
, clickOK
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
ofAppProvider
, add reference tomaven-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
, selectNew --> Module
- Select
Maven
from the left-panel, do NOT checkCreate from archetype
on the right-panel, then pressNext
- Input the artifactId to be
service
, and clickNext
- Review the values and click
Finish
- Check
pom.xml
ofservice
, to see that there is a<parent></parent>
definition - Check
pom.xml
ofparent-project
, to see that onemodule
has been added automatically - Right-click on
service --> src
, selectMark Directory as --> Sources Root
- Right-click on
service --> src --> main --> java
(its icon has been changed now), selectNew --> Java Class
- Name the class as
SampleService
, clickOK
- Add a public method to
SampleService
, simply print another message
mj0025.png
Step 5 Make provider
to depend on service
-
Open
AppProvider
under moduleprovider
-
In the main-method, type
SampleService
, pressAlt + Enter
and select toAdd 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 tomvn 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

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

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