简体   繁体   中英

Packaging a Jar with no third party dependencies in Maven

I have created several projects in Java that I would like to package as group. I have created a wrapper project to hold these. So we pretty much have a set up like this

  • Project 1 - Has dependencies on ThirdPartyProject1
  • Project 2 - Has dependencies on ThirdPartyProject2
  • Project 3 - Has dependencies on ThirdPartyProject3
  • WrapperProject - Has dependencies on Project1, Project2, and Project3.

We are packaging a project that uses the WrapperProject and my supervisors would like to package the third party dependencies in separate file than the WrapperProject.jar that would be produced during the assembly.

I wonder whether that is possible and how? I have been looking into the Maven dependency plugin but have not used it before and so I am not quite sure how it works. At the end of the day I would like to have a lib folder and a Jar file that would look something like this.

Lib - ThirdPartyProject1.jar - ThirdPartyProject2.jar - ThirdPartyProject3.jar

WrapperProject.jar - Project1.jar - Project2.jar - Project3.jar

You can use the copy resources plugin to make a /target/lib folder. Or you can use the assembly plugin to create a jar/zip of all the dependencies. I'm not sure what you are doing with the lib directory, but it might be more convenient to have it as a zip file.

If the Third party jar is available in maven repository, then you can use normal maven build command "mvn package", or else the jar is project specific custom one then you should install that jar into your maven local repository using the command "mvn install". Refer the below link,

http://www.mkyong.com/maven/how-to-include-library-manully-into-maven-local-repository/

Once done then include that dependency and the below build configuration into your pom.XML, Then change the main-class.

 <build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>fully.qualified.MainClass</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build> 

Now build the project then you will get two jar one is without dependency & another one is with dependency classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM