Search code examples
javamavenintellij-ideajar

How to force Maven in IntelliJ to pull specific dependency from local .m2 repository


Introduction

In a specific project I inherited we have a dependency as a jar file that is a custom wrapper/servlet for simple-capcha. They told me to make specific changes to the small wrapper capcha project, and produce the JAR - then test it in the main project.

Then I had to get the second main project to use the new JAR as the dependency (instead of the file located on the company artifactory).

Current Process

Below I describe the process I am using that I feel is very inefficient. I produce using maven the jar file from the small project. I then go to the local .m2 repository and replace the JAR that exists there (downloaded from artifactory) with the new one.

<dependency>
            <groupId>test.company.something.captcha</groupId>
            <artifactId>captcha-common</artifactId>
            <version>0.90</version>
        <!--    <scope>system</scope>
            <systemPath>C:/Users/mbakopoulos/Dropbox/_captcha/target/captcha-common-0.90.jar</systemPath>-->
        </dependency>

Finally, I uncomment, and comment the <scope> and <systemPath> tags (with auto-import enabled) so that intellij detects the 2 changes, and forces maven to pulls from the local .m2 repository replacing the maven:library in the project.

Finally, the specific JAR contains a HTTPServlet that is actually being used in the web.xml of the main project. At some point when I changed the dependency version I was getting a classNotFoundException after JBOSS deployed the war:

<servlet>
        <servlet-name>TheCaptcha</servlet-name>
        <servlet-class><servlet>
    <servlet-name>StickyCaptcha</servlet-name>
    <servlet-class>test.company.something.CaptchaServerServlet</servlet-class>
    <init-param>
        <param-name>width</param-name>
        <param-value>250</param-value>
    </init-param>
    <init-param>
        <param-name>height</param-name>
        <param-value>60</param-value>
    </init-param>       
</servlet>

Question

What is the best way to develop and test a dependency that was/is included usually as a dependency? Could I get rid of the pom.xml entry, and include the other project as a module?

I am using intelliJ IDEA 2016.

Finally, Is there anyway to connect to two modules/projects together? I suspect there should be.


Solution

  • Moving my comments to an extended answer here.

    Firstly, having multiple different versions of any released maven artifact with the same version number is asking for a lot of problems!! If at all possible, avoid doing this. The numbering system can always be extended (e.g. 0.90.1 or 0.90-CUSTOM-1 or 0.09-A).

    Secondly, using systemPath for a dependency is non-standard. Try to work with versions "normally", so that they live in your M2_HOME\.m2\repository when working locally (and can be uploaded to your artifactory or nexus to be shared with colleagues).

    Now, as far as automating the build / test process, you have a few options. You can either build 0.90.1 and 0.90.2 and update the version for the <dependency> section every time the dependency is rebuilt.

    Or you can use snapshot versioning. 0.90.1-SNAPSHOT indicates to maven that it's a snapshot build. Whenever you rebuild a project with a snapshot dependency, it will look to see if that dependency has been changed.

    Lastly you could move the dependency into the same project as your main project as a submodule. Then you avoid having to define an explicit dependency between projects.

    Hope this helps...