繁体   English   中英

将常规的Maven项目转换为Spring Boot项目

[英]Converting a regular Maven project to a Spring Boot project

我的主管要求我将一个旧的Maven项目转换为一个Spring Boot项目,以便我们能够通过RESTful交互访问该项目的后端(在该项目的后端只能通过控制台界面访问之前)。

因此,首先,我在单独的项目包中添加了一个简单的Spring Boot应用程序。 之后,我开始通过Spring Boot所需的依赖项扩展项目的pom.xml,并调整了整个项目设置。 现在,我尝试运行旧项目的后端,事实证明它正在运行。 但是,简单的Spring Boot应用程序没有。

我将问题缩小为pom.xml的“旧”部分中的冲突依赖项:

<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.6.1</version>
</dependency>

当我将此依赖项保留在pom.xml中时,旧的后端可以工作,但是Spring Boot应用程序失败并出现以下错误:

WARN: Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)

如果我注释掉这种依赖关系,Spring Application可以正常工作,但是旧的后端将失败。 我使用spring-boot-admin-starter-server 2.0.4.RELEASE版本。 我认为该日志记录包的旧后端版本与spring-boot-admin-starter-server包含的版本不同。 但是,我在项目中某种程度上需要两个版本。

不可能的:

  • 更新旧资源,因为其中一些拥有外部公司的所有权

我已经尝试过,但是在以下方面没有成功:

  • 从Spring Boot代理中排除日志记录。 这将导致以下错误:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

  • 正如我的网络研究中所建议的那样,我还尝试使用shade插件。 不幸的是,我无法用这种方法解决问题。

有人建议如何解决这个问题吗? 我会很感激。 我不习惯解决这种依赖问题。 如果我缺少明显的东西,请原谅。

-lema

编辑pom.xml(不幸的是,我不得不省去它的大部分):

<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>

... 

<packaging>jar</packaging>
<description></description>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-boot-admin.version>2.0.2</spring-boot-admin.version>
    <spring-boot-dependencies.version>2.0.4.RELEASE</spring-boot-dependencies.version>

    ...

    <rat.skip>true</rat.skip>
    <log4j-version>2.6.1</log4j-version>
</properties>

<repositories>
    ...
</repositories>

<dependencyManagement>
    <dependencies>
        <!-- Necessary dependency for running Spring Boot without starter parent -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>Fowler-SR2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot-dependencies.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        ...

    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-server</artifactId>
        <version>${spring-boot-admin.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    ...

    <!-- TODO The version of this dependency lets Spring Boot fail, but is 
        necessary tu run the old backend -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j-version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j-version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-iostreams</artifactId>
        <version>${log4j-version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-1.2-api</artifactId>
        <version>${log4j-version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-jcl</artifactId>
        <version>${log4j-version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-jul</artifactId>
        <version>${log4j-version}</version>
    </dependency>
    <dependency>
        <groupId>org.fusesource.jansi</groupId>
        <artifactId>jansi</artifactId>
        <version>1.13</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
    </dependency>

    ...

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


        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>prepare-config-zip</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptors>
                            <descriptor>${basedir}/src/main/assembly/config.xml</descriptor>
                        </descriptors>
                        <finalName>configs</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                    </configuration>
                </execution>
                <execution>
                    <id>prepare-dist-zip</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptor>src/main/assembly/dist.xml</descriptor>
                        <finalName>...</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <artifactId>maven-enforcer-plugin</artifactId>
            <configuration>
                <rules>
                    <requireJavaVersion>
                        <version>1.8</version>
                    </requireJavaVersion>
                </rules>
            </configuration>
        </plugin>

    </plugins>
</build>
<profiles>
    <profile>
        <id>attach-standalone</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <configuration>
                        <shadedArtifactAttached>true</shadedArtifactAttached>
                        <shadedClassifierName>standalone</shadedClassifierName>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>dont-attach-standalone</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <configuration>
                        <!-- Prevent huge shaded artifacts from being deployed to Artifactory -->
                        <outputFile>...</outputFile>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

编辑:我刚刚发现,如果删除冲突依赖关系中的version元素,Spring Boot Application可以正常工作,但是不幸的是,后端失败了。

所以我找到了一个解决方案,但这可能不是最好的方法:

我只是将<spring-boot-dependencies.version>2.0.4</spring-boot-dependencies.version>替换为与冲突日志依赖项兼容的旧版本,即版本1.4.7.RELEASE

这是Spring Boot应用程序和后端同时工作的最新版本(通过尝试并发现错误)。

无论如何,非常感谢您的帮助。

干杯

暂无
暂无

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

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