简体   繁体   English

spring-boot-starter-jetty和Jetty模块-Maven Magic

[英]spring-boot-starter-jetty and Jetty modules - Maven magic

We're using spring-boot-starter-jetty in our maven pom, which means we don't have any direct dependency on jetty hence no control on its version. 我们在maven pom中使用spring-boot-starter-jetty,这意味着我们对码头不直接依赖,因此无法控制码头的版本。

This all works good, but we now need to add a dependency onone of jetty's modules, which are using the same jetty version convention. 这一切都很好,但是我们现在需要添加对使用相同码头版本约定的码头模块之一的依赖。

This is all great and dandy, but because we can't use spring-boot-starter-jetty as our root pom, we're declaring jetty's version again under the module's dependency declaration, something like this: 这一切都很棒,但由于我们不能使用spring-boot-starter-jetty作为我们的根pom,因此我们在模块的依赖项声明下再次声明了jetty的版本,如下所示:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jetty</artifactId>
   <version>1.4.3-RELEASE</version>
</dependency>
<dependency>
   <groupId>org.eclipse.jetty</groupId>
   <artifactId>jetty-nosql</artifactId>
   <version>9.3.14.v20161028</version>
</dependency>

This is no fun PLUS dangerous as if we upgrade one of them, we have to remember to upgrade the latter to the same version. 这没什么好玩的PLUS危险,就好像我们升级其中之一一样,我们必须记住将后者升级到相同版本。

I know we can add an exclude under the module's dependency but this feels like a hack plus still doesn't fully protect us from breaking changes. 我知道我们可以在模块的依赖项下添加一个exclude,但这感觉像是一个hack +仍然不能完全保护我们免受破坏。

Is there anyway to extract the jetty version from spring boot and reuse it for the module's dependency? 无论如何,是否有必要从Spring Boot中提取码头版本并将其重新用于模块的依赖关系?

Thanks!! 谢谢!!

Maven creates the version properties for it's dependencies in the effective POM . Maven在effective POM为其依赖项创建版本属性。 For jetty it's jetty.version 对于码头来说就是jetty.version

So your dependencies will become 所以你的依赖将变成

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-nosql</artifactId>
        <version>${jetty.version}</version>
    </dependency>

Here's complete POM 这是完整的POM

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>TestDependency</groupId>
    <artifactId>TestDependency</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>TestDependency</name>
    <description>TestDependency</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-nosql</artifactId>
            <version>${jetty.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

It's worth pointing out that this is not required with the latest version of Spring Boot . 值得指出的是,最新版本的Spring Boot不需Spring Boot I checked with the 1.5.4.RELEASE . 我检查了1.5.4.RELEASE Following is suficient. 跟踪就足够了。

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-nosql</artifactId>
    </dependency>

Complete POM 完整的POM

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>TestDependency</groupId>
    <artifactId>TestDependency</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>TestDependency</name>
    <description>TestDependency</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-nosql</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

Effective POM can be generated by using following command 使用以下命令可以生成有效的POM

mvn help:effective-pom

Or if you're using Spring Tool Suite or latest version of Eclipse then you can directly check it inside the bottom tab in the Maven POM Editor 或者,如果您使用的是Spring Tool Suite或最新版本的Eclipse,则可以在Maven POM Editor的底部选项卡中直接检查它

在此处输入图片说明

Edit 编辑

Without spring-boot parent pom the same can be done using dependencyManagement 如果没有spring-boot父pom,则可以使用dependencyManagement完成相同操作

Following is the complete pom without spring-boot parent 以下是没有spring-boot父级的完整pom

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>TestDependency</groupId>
    <artifactId>TestDependency</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>TestDependency</name>
    <description>TestDependency</description>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>1.4.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-nosql</artifactId>
            <version>${jetty.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

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

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