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
mj0001.pngFile --> New --> Project
-
In the popped 'New Project' windows, select
mj0002.pngMaven
from the left-panel, do NOT checkCreate from archetype
on the right-panel, then pressNext
-
Input
mj0003.pngGroupId
,ArtifactId
for the project and pressNext
-
Review the properties of the project and press
mj0004.pngFinish
-
Review the project created
mj0005.png -
Right-click on the
mj0006.pngsrc
folder and selectDelete
to delete thesrc
folder from the parent-project, since it doesn't need any code
mj0007.png -
Review the current status of the parent-project
mj0008.png -
In the file
mj0009.pngpom.xml
, define the packaging of the parent-project to bepom
Step 2 Create the main-entry
-
Right-click on
mj0010.pngparentproject
, selectNew --> Module
-
Select
mj0011.pngMaven
from the left-panel, do NOT checkCreate from archetype
on the right-panel, then pressNext
-
Input the artifactId and click
mj0012.pngNext
-
Review the values and click
mj0013.pngFinish
-
Check
mj0014.pngpom.xml
ofprovider
, to see that there is a<parent></parent>
definition
-
Check
mj0015.pngpom.xml
ofparent-project
, to see that onemodule
has been added automatically
-
Right-click on
mj0016.pngprovider --> src
, selectMark Directory as --> Sources Root
-
Right-click on
mj0017.pngprovider --> src --> main --> java
(its icon has been changed now), selectNew --> Java Class
-
Name the class as
mj0018.pngAppProvider
, clickOK
-
Add a main() method to
mj0019.pngAppProvider
, simply print a message
-
Click the
mj0020.pngrun
icon to run the main method
-
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
mj0021.pngmvn clean compile
-
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
mj0023.pngpom.xml
ofAppProvider
, add reference tomaven-jar-plugin
and specify the class-path & main-Class
-
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
mj0026.pngSampleService
, pressAlt + Enter
and select toAdd dependency on module 'service'
-
Finish the code to invoke the method from
mj0027.pngSampleService
-
Back to command-line, change directory to folder of
mj0028.pngparent-project
, try tomvn install
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
mj0031.pngprovider\target
and try execute the JAR
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...
网友评论