简体   繁体   中英

Issue creating and using a custom jar file

I have prepared some util classes. I planned to make them as jar and distribute it to required projects.

My util classes uses some already existing custom code provided in the form of jar file.

My code is dependent on "MainUtil.jar" whi internally dependends on Java Servlet, Commons IO, Commons Codec and so on.....

My POM dependency looks as below.

<dependency>
    <groupId>com.solutions</groupId>
    <artifactId>sol-core</artifactId>
    <version>2.3</version>          
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-ws-security</artifactId>
    <version>${cxf.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-ws-policy</artifactId>
    <version>${cxf.version}</version>
</dependency>
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk16</artifactId>
    <version>1.46</version>
</dependency>

When I package my jar it looks fine.

But when my jar is used in a project where these my util classes are used , I could see a wierd issue.

The commonc-codec jar files are not included in the project package when packaged. Also code which requies this common-codec is failing.

When I explicitly include the commons-codec dependency, everything works perectly.

My confusion is, why should I explicitly add the codec dependency when I should be resolved by Maven based on the POM of the custom jar files. And why the issue is happening only with the commons-codec but not with other dependency.

Your code depends on all the other jars. When you create jar for your project the jar file does not contain all the dependent jar classes.

Where ever you are using your jar you have to use other dependent jars. You have not mentioned whether you are using maven there also. If yes then if you have defined dependency then all the dependent jars will be in the classpath.

Issue with you dependency resolving is,

the existing dependency in your project might have some dependency management on this jar. That is the reason, old jar is taking precedence over your custom jar dependency. Thry adding exclusion in your already existing jar for this common-codec jar.

like

<dependency>
 <... Your existing dependency ..>
     <exclusions>
            <exclusion>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
            </exclusion>
        </exclusions>
</dependency>

Use this command and check how your dependency is being resolved.

 mvn dependency:resolve

Then everything should be fine.

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