How to add a local non-maven project as a dependency for a maven project?

后端 未结 3 1179
难免孤独
难免孤独 2020-12-21 04:36

I created a new Spring web app, and I\'d very much like to use Maven to handle builds/dependencies. My problem is that the project depends on some existing local projects,

相关标签:
3条回答
  • 2020-12-21 05:10

    No and you shouldn't try that. Maven's main goal is to have a standardized build. This is what makes Maven so easy to use: Exceptions aren't be the norm. That's why Maven requires that all dependencies are in the typical Maven format (POM + jar).

    When you say "mavenizing them is not currently an option", you basically say "Maven is not currently an option". In my experience, converting an existing project to Maven just takes a couple of minutes ... unless said project uses a messy, unreliable build process in which case you really should convert it to Maven ASAP just to stop wasting even more of your precious time. :-)

    If everything else fails, use a build server to produce JAR files from the dependent projects and deploy them to a company wide repository server or just to your local machine.

    0 讨论(0)
  • 2020-12-21 05:22

    This answer's a tad late but just in case somebody needs it, I found the solution using Maven's Build Helper Plugin, that is if you don't want deploying your non-maven project as a jar:

    So basically you just add the source directory of the non-maven project in the <sources> tag:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
            <execution>
                <id>add-source</id>
                <phase>generate-sources</phase>
                <goals>
                    <goal>add-source</goal>
                </goals>
                <configuration>
                    <sources>
                        <source>C:\Users\user123\eclipse-workspace\my_non_maven_project\src</source>
                    </sources>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    Now if that non-maven project has dependencies of its own, you need to add them in the <dependencies> section of your maven project as well.

    0 讨论(0)
  • 2020-12-21 05:29

    I think that the easiest way to do this is to deploy your local dependency to your local repository so that maven can use it as regular dependency.

    To deploy local jar file into local repository use command line like:

    mvn install:install-file -Dfile=<MY-FILE> -DgroupId=MY-GROUP-ID -DartifactId=MY-ARGIFACT -Dversion=MY-VERSION -Dpackaging=jar
    

    If you have shared repository like Artifactory in your company you can deploy the jar there. Otherwise each developer will have to deploy in into his local repository. The good news it must be done only once.

    EDIT.

    Here is a way to define project-to-project dependency into pom.xml

        <dependency>
            <groupId>com.mycompany.util</groupId>
            <artifactId>util</artifactId>
            <version>${com.mycompany.version}</version>
        </dependency>
    

    That's it. Util is just yet another project into my workspace.

    0 讨论(0)
提交回复
热议问题