简体   繁体   English

构建Maven Java项目时如何包含其他Java项目

[英]How to include other Java Project when build Maven Java Project

I have two projects, Java Project and Maven Java Project .我有两个项目, Java ProjectMaven Java Project I build my Maven Java Project as runnable jar file using this pom setting.我使用这个 pom 设置将我的 Maven Java 项目构建为可运行的 jar 文件。

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <finalName>ISO8583PostpaidServer</finalName>
                <!-- <finalName>ISO8583PrepaidServer</finalName> -->
                <archive>
                    <manifest>
                        <!-- If you want to compile as Prepaid change this class reference -->
                        <mainClass>PostpaidServer</mainClass>
                        <!-- <mainClass>PrepaidServer</mainClass> -->
                    </manifest>
                </archive>

                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>

                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

    </plugins>
  </build>

But i get an error because in my Build Path i add dependency to my Java Project .但是我收到一个错误,因为在我的 Build Path 中我添加了对Java Project 的依赖。 How to add standard Java Project as dependency to my Maven Project?如何将标准 Java 项目作为依赖项添加到我的 Maven 项目?

Thank you anyway :)还是很感谢你 :)

As you state, your problem is that you depend on a Java Project that is not managed with maven.正如您所说,您的问题是您依赖于未使用 maven 管理的 Java 项目。

I think you have two options:我认为你有两个选择:

  • Convert that project to a maven project, and create a multimodule Maven project.将该项目转换为 Maven 项目,并创建一个多模块 Maven 项目。 This is convenient if you are the owner of the other project.如果您是另一个项目的所有者,这会很方便。
  • Build that project, take jar and create your own local maven repo.构建该项目,获取jar并创建您自己的本地 Maven 存储库。 This is convenient in case you can't manage the other project, or you just want to use as a dependency.这在您无法管理其他项目或只想用作依赖项的情况下很方便。

1. CONVERT TO MAVEN AND CREATE MULTIMODULE 1.转换为Maven并创建多模块

If you have the project source, and you can change it because it is on your own, the best choice is:如果您有项目源,并且您可以更改它,因为它是您自己的,最好的选择是:

  • Transform to a maven project转换为 Maven 项目
  • Create a parent maven project with two modules: your project and your transformed project.创建一个包含两个模块的父 maven 项目:您的项目和转换后的项目。
  • Add dependency.添加依赖。

Transform to a maven project转换为 Maven 项目

It is not difficult to transform a Java Standard project to a maven project:将 Java Standard 项目转换为 maven 项目并不难:

  • Change source and resources paths to src/main/java/ and src/main/resources/ .将源和资源路径更改为src/main/java/src/main/resources/
  • Declare dependencies.声明依赖关系。
  • Configure plugins.配置插件。

If you are the owner, and controls that project, you can transform it in a few minutes.如果您是所有者并控制该项目,则可以在几分钟内对其进行改造。 If you don't have control on the project, it could a tedious task.如果您无法控制项目,这可能是一项乏味的任务。

Create a parent project创建父项目

Once you get transformed your dependent project, create a new project structure as follows:转换依赖项目后,创建一个新的项目结构,如下所示:

parent
 ├── module1
 │   ├── src ...
 │   └── pom.xml
 ├── module1
 │   ├── src ...
 │   └── pom.xml
 └── pom.xml

your parent pom.xml will declare a modules section to include both modules, and your modules pom.xml will include a parent section to reference parent project.您的父pom.xml将声明一个modules部分以包含两个模块,您的模块pom.xml将包含一个parent部分以引用父项目。

Parent pom父 pom

<project 
    xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
        http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>GROUP</groupId>
    <artifactId>PARENT</artifactId>
    <packaging>pom</packaging>
    <version>VERSION</version>

    ...

    <modules>
        <module>module1</module>
        <module>module2</module>
    </modules>

    ...

Modules pom模块 pom

<project 
    xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
        http://maven.apache.org/maven-v4_0_0.xsd">

    <parent>
        <groupId>GROUP</groupId>
        <artifactId>PARENT</artifactId>
        <version>VERSION</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>MODULE1</artifactId>
    <packaging>jar</packaging>

Declare dependency声明依赖

Finally declare the dependency to module2 on module1最后在module1上声明对module2的依赖

<dependency>
    <groupId>GROUP</groupId>
    <artifactId>MODULE2</artifactId>
    <version>VERSION</version>
</dependency>

2. OWN LOCAL MAVEN REPO OPTION 2. 自己的本地 Maven 回购选项

In this case, the most interesting option is to deploy your dependent jar on a private/corporate Nexus or Artifactory.在这种情况下,最有趣的选择是将您的依赖jar部署在私人/企业 Nexus 或 Artifactory 上。

But, If you don't have access to a private/corporate repository, you can use a local maven repository in your project.但是,如果您无权访问私人/公司存储库,则可以在项目中使用本地 Maven 存储库。 This way has an advantage, because all programmers gets all dependencies resolved with no command.这种方式有一个优势,因为所有程序员都无需命令即可解决所有依赖项。

Create your local repository创建本地存储库

First, you have to create a folder to store local maven repository.首先,您必须创建一个文件夹来存储本地 Maven 存储库。 I will do it in the root path of your repository, for example call it local-libs .我将在您的存储库的根路径中执行此操作,例如将其称为local-libs

Then, when you want to add a private jar, run the following command:然后,当您要添加私有 jar 时,请运行以下命令:

mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file \
    -Dfile=YOU_FILE.jar \
    -DgroupId=GROUP \
    -DartifactId=ARTIFACT \
    -Dversion=VERSION \
    -Dpackaging=jar \
    -DlocalRepositoryPath=local-libs

This command will store your jar into your local maven repository ( and not in your HOME/.m2/repository ) as GROUP.ARTIFACT.VERSION .此命令会将您的 jar 存储到您的本地 maven 存储库(而不是您的HOME/.m2/repository )作为GROUP.ARTIFACT.VERSION

Declare local maven repo on your projects pom在你的项目 pom 上声明本地 Maven 仓库

After you created your local maven repo, declare it in your pom.xml as follows:创建本地 maven 存储库后,在pom.xml声明它,如下所示:

<repositories>
    <repository>
        <id>local-repo</id>
        <name>Local Repo</name>
        <url>file://${project.basedir}/local-libs</url>
    </repository>
</repositories>

Declare dependency声明依赖

Once you have created and declared your local maven repo, use it.创建并声明本地 maven 存储库后,请使用它。 Create a dependency as follows:创建一个依赖如下:

<dependency>
    <groupId>GROUP</groupId>
    <artifactId>ARTIFACT</artifactId>
    <version>VERSION</version>
</dependency>

And you don't have to do anything else.而且您无需执行任何其他操作。

Once you created your local maven repo, every programmer that gets your repo can work immediately, it is not needed to run again any mvn command.一旦你创建了你的本地 maven repo,每个获得你的 repo 的程序员都可以立即工作,不需要再次运行任何mvn命令。

The easiest method with Eclipse is: Eclipse 最简单的方法是:

  1. Convert the Java projects to Maven project, Configure->Convert to Maven Project .将 Java 项目转换为 Maven 项目, Configure->Convert to Maven Project Fill in Group Id , Artifact Id and version .填写Group IdArtifact Idversion

  2. Run As->Maven install

  3. In your project which wants to use them, add the above created dependency information in pom.xml在你想要使用它们的项目中,在pom.xml添加上面创建的依赖信息

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

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