【问题标题】:Converting a regular Maven project to a Spring Boot project将常规 Maven 项目转换为 Spring Boot 项目
【发布时间】:2019-02-18 05:18:23
【问题描述】:

我的主管要求我将一个旧的 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 应用程序完全可以正常工作,但旧的后端会失败。我使用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.

  • 我还尝试按照我的网络研究中的一些建议使用阴影插件。不幸的是,我无法用这种方法解决问题。

有没有人建议如何解决这个问题?我会很感激。我不习惯解决这种依赖问题。如果我遗漏了一些明显的东西,请原谅。

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

编辑:我刚刚发现,如果您删除冲突依赖项中的版本元素,Spring Boot 应用程序可以工作,但不幸的是后端会失败。

【问题讨论】:

  • 只是为了验证您对冲突日志包的假设是否正确,您可以排除 pom 文件中的 spring-boot-admin-starter-server 吗?我们只是想消除一些依赖关系以隔离问题并找出原因。你真的不需要spring-boot-admin-starter-server 来使项目成为弹簧启动。
  • 哦,对不起。我忘了提到我没有从&lt;parent&gt; 中的 Spring Boot Starter 继承,因为父级被旧后端继承的另一个单元占用。如果我在没有 spring-boot-admin-starter-server 的情况下运行 Spring 应用程序,我会得到:Exception in thread "main" java.lang.Error: Unresolved compilation problem: SpringApplication cannot be resolved at ...restAdapter.RestfulServerLauncher.main(RestfulServerLauncher.java:10)
  • 你必须让 spring boot 应用在没有你的后端依赖的情况下工作。然后一次添加一个依赖项并使其运行并检查是否有问题。可以分享一下 pom 文件吗?
  • 首先。谢谢你的帮助。 ;) 如上所述,只要我注释掉冲突的依赖项,spring boot 应用程序就可以运行得很好。就像您建议的那样,我一次添加了一个后端依赖项。这就是导致我得出的结论,即日志记录依赖性必须是问题的根源。我会提供 pom.xml。

标签: java spring maven spring-boot


【解决方案1】:

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

我刚刚将 &lt;spring-boot-dependencies.version&gt;2.0.4&lt;/spring-boot-dependencies.version&gt; 替换为与冲突的日志记录依赖项兼容的旧版本,即版本 1.4.7.RELEASE

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

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

干杯

【讨论】:

    猜你喜欢
    • 2015-06-04
    • 2017-10-19
    • 2012-07-24
    • 2019-12-16
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    相关资源
    最近更新 更多