简体   繁体   English

如何添加本地非maven项目作为maven项目的依赖?

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

I created a new Spring web app, and I'd very much like to use Maven to handle builds/dependencies.我创建了一个新的 Spring Web 应用程序,我非常想使用 Maven 来处理构建/依赖项。 My problem is that the project depends on some existing local projects, and mavenizing them is not currently an option.我的问题是该项目依赖于一些现有的本地项目,目前还不能选择对它们进行 mavenizing。 Is it possible to depend on these projects without mavenizing them?是否有可能依赖这些项目而不对其进行 mavenizing?

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.我认为最简单的方法是将本地依赖项部署到本地存储库,以便 maven 可以将其用作常规依赖项。

To deploy local jar file into local repository use command line like:要将本地jar文件部署到本地存储库,请使用如下命令行:

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.如果你的公司有像 Artifactory 这样的共享存储库,你可以在那里部署 jar。 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这是一种将项目到项目依赖项定义到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. Util 只是我工作区的另一个项目。

No and you shouldn't try that.不,你不应该尝试那个。 Maven's main goal is to have a standardized build. Maven 的主要目标是标准化构建。 This is what makes Maven so easy to use: Exceptions aren't be the norm.这就是 Maven 如此易于使用的原因:异常不是常态。 That's why Maven requires that all dependencies are in the typical Maven format (POM + jar).这就是为什么 Maven 要求所有依赖项都采用典型的 Maven 格式(POM + jar)。

When you say "mavenizing them is not currently an option", you basically say "Maven is not currently an option".当您说“mavenizing 它们目前不是一种选择”时,您基本上是说“Maven 目前不是一种选择”。 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.根据我的经验,将现有项目转换为 Maven 只需要几分钟......除非该项目使用混乱、不可靠的构建过程,在这种情况下,您真的应该尽快将其转换为 Maven,以免浪费更多宝贵的时间. :-) :-)

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.如果其他一切都失败了,请使用构建服务器从依赖项目生成 JAR 文件,并将它们部署到公司范围的存储库服务器或仅部署到您的本地机器。

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:这个答案有点晚了,但以防万一有人需要它,我找到了使用 Maven 的Build Helper Plugin的解决方案,也就是说,如果您不想将非 Maven 项目部署为 jar:

So basically you just add the source directory of the non-maven project in the <sources> tag:所以基本上你只需在<sources>标签中添加非 maven 项目的源目录:

<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.现在,如果该非 Maven 项目有自己的依赖项,则还需要将它们添加到 Maven 项目的<dependencies>部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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