简体   繁体   English

使用 ant 编译 Spring-Boot 应用程序时出错

[英]Error when compile Spring-Boot app with ant

I am trying to compile a Spring Boot + Batch project with AntLib since in CI Jenkins it is configured this way.我正在尝试使用 AntLib 编译 Spring Boot + Batch 项目,因为在 CI Jenkins 中它是这样配置的。

When compiling the project, an error is generated "/ext-lib/spring-boot-antlib-2.4.0.jar./org/springframework /boot/ant/antlib:xml:27. java.lang:NoClassDefFoundError: org / springframework / util / StringUtils " When compiling the project, an error is generated "/ext-lib/spring-boot-antlib-2.4.0.jar./org/springframework /boot/ant/antlib:xml:27. java.lang:NoClassDefFoundError: org / springframework / util / StringUtils "

The build.xml build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="gprd-logs-cleaner" default="main" basedir="."
    xmlns:au="antlib:org.apache.ant.antunit" 
    xmlns:spring-boot="antlib:org.springframework.boot.ant" 
    xmlns:ivy="antlib:org.apache.ivy.ant">

    <property name="src.dir" location="src/main/java" />
    <property name="resource.dir" location="src/main/resources" />
    <property name="target.dir" location="target" />
    <property name="classes.dir" location="${target.dir}/classes" />
    <property name="lib.dir" value="lib" />
    <property name="main-class" value="com.gprd.logs.cleaner.CCCLogsCleanerApp" />

    <taskdef resource="org/apache/ivy/ant/antlib.xml"
            uri="antlib:org.apache.ivy.ant" classpath="${ivy.class.path}"/>
    <taskdef resource="org/apache/ant/antunit/antlib.xml"
            uri="antlib:org.apache.ant.antunit" classpath="${antunit.class.path}"/>

    <path id="classpath">
        <fileset dir="${basedir}/">
            <include name="${lib.dir}/*.jar" />
        </fileset>
    </path>

    <target name="setUp">
        <mkdir dir="${classes.dir}" />
        <javac srcdir="${src.dir}" includeantruntime="false" destdir="${classes.dir}" classpathref="classpath"/>
    </target>

    <target name="compile" depends="setUp">
        <local name="jar" />
        <property name="jar" location="${target.dir}/gprd-logs-cleaner.jar" />
        <spring-boot:exejar destfile="${jar}" classes="${classes.dir}">
            <resources>
                <fileset dir="${resource.dir}" />
            </resources>
            <lib>
                <path refid="classpath" />
            </lib>
        </spring-boot:exejar>
    </target>
    
    <target name="clean">
        <delete dir="${target.dir}" />
    </target>
    
    <!-- Default, run this -->
    <target name="main" depends="clean, compile" />
    
</project>

Spring Boot version: 2.3.4.RELEASE . Spring 引导版本: 2.3.4.RELEASE

I tested with SpringBootAndlib version: spring-boot-antlib-2.4.4.jar or spring-boot-antlib-2.4.0.jar , the error is the same.我用 SpringBootAndlib 版本测试: spring-boot-antlib-2.4.4.jarspring-boot-antlib-2.4.0.jar ,错误是一样的。

Library list:图书馆清单:

spring-boot-starter-2.3.4.RELEASE.jar               istack-commons-runtime-3.0.11.jar
spring-boot-2.3.4.RELEASE.jar                       jakarta.activation-1.2.2.jar
spring-boot-autoconfigure-2.3.4.RELEASE.jar         spring-data-jpa-2.3.4.RELEASE.jar
spring-boot-starter-logging-2.3.4.RELEASE.jar       spring-data-commons-2.3.4.RELEASE.jar
logback-classic-1.2.3.jar                           spring-orm-5.2.9.RELEASE.jar
logback-core-1.2.3.jar                              slf4j-api-1.7.28.jar
log4j-to-slf4j-2.13.3.jar                           spring-aspects-5.2.9.RELEASE.jar
log4j-api-2.13.3.jar                                hibernate-entitymanager-5.4.5.Final.jar
jul-to-slf4j-1.7.28.jar                             jboss-logging-3.4.1.Final.jar
jakarta.annotation-api-1.3.5.jar                    dom4j-2.1.1.jar
spring-core-5.2.9.RELEASE.jar                       hibernate-commons-annotations-5.1.0.Final.jar
spring-jcl-5.2.9.RELEASE.jar                        javax.persistence-api-2.2.jar
snakeyaml-1.26.jar                                  byte-buddy-1.10.14.jar
spring-boot-starter-data-jpa-2.3.4.RELEASE.jar      jboss-transaction-api_1.2_spec-1.1.1.Final.jar
spring-boot-starter-aop-2.3.4.RELEASE.jar           spring-batch-core-4.2.4.RELEASE.jar
aspectjweaver-1.9.6.jar                             jackson-databind-2.11.2.jar
spring-boot-starter-jdbc-2.3.4.RELEASE.jar          jackson-annotations-2.11.2.jar
HikariCP-3.4.5.jar                                  jackson-core-2.11.2.jar
jakarta.transaction-api-1.3.3.jar                   micrometer-core-1.5.5.jar
jakarta.persistence-api-2.2.3.jar                   HdrHistogram-2.1.12.jar
hibernate-core-5.4.5.Final.jar                      LatencyUtils-2.0.3.jar
javassist-3.24.0-GA.jar                             javax.batch-api-1.0.jar
antlr-2.7.7.jar                                     jettison-1.2.jar
jandex-2.0.5.Final.jar                              spring-batch-infrastructure-4.2.4.RELEASE.jar
classmate-1.5.1.jar                                 spring-retry-1.2.5.RELEASE.jar
jaxb-runtime-2.3.3.jar                              spring-aop-5.2.9.RELEASE.jar
txw2-2.3.3.jar                                      spring-beans-5.2.9.RELEASE.jar
spring-context-5.2.9.RELEASE.jar                    spring-expression-5.2.9.RELEASE.jar
spring-tx-5.2.9.RELEASE.jar                         spring-jdbc-5.2.9.RELEASE.jar
jakarta.xml.bind-api-2.3.3.jar                      jakarta.activation-api-1.2.2.jar
jasypt-spring-boot-starter-2.0.0.jar                jasypt-spring-boot-2.0.0.jar
jasypt-1.9.2.jar

NOTE: We cannot connect to download dependencies.注意:我们无法连接到下载依赖项。 Thanks in advance提前致谢

java.lang.NoClassDefFoundError: org / springframework / util / StringUtils " java.lang.NoClassDefFoundError: org / springframework / util / StringUtils "

This error means the class org.springframework.util.StringUtils is not found.此错误表示未找到 class org.springframework.util.StringUtils This class is located in the spring-core module.此 class 位于spring-core模块中。

You have spring-core-5.2.9.RELEASE.jar in the list you shared, but it does not seem to be in your classpath.您共享的列表中有spring-core-5.2.9.RELEASE.jar ,但它似乎不在您的类路径中。 You need to make sure that this jar is in your classpath.您需要确保此 jar 在您的类路径中。

Ok, I have solved the problem.好的,我已经解决了这个问题。 It already compiles completely, but there are some adjustments to be made.它已经完全编译了,但是需要进行一些调整。 First you have to indicate where the libraries are by running the command with ant -debug -lib./ext-lib/ -lib. /lib/首先,您必须通过运行带有ant -debug -lib./ext-lib/ -lib. /lib/ ant -debug -lib./ext-lib/ -lib. /lib/ . ant -debug -lib./ext-lib/ -lib. /lib/ I have extracted from the spring-boot-antlib jar part of the xml and adjusted to my needs.我从 xml 的 spring-boot-antlib jar 部分中提取并根据我的需要进行了调整。 Here I leave you in case it is useful to someone.在这里我离开你,以防它对某人有用。

build.xml build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="gprd-logs-cleaner" default="main" basedir=".">

    <property name="src.dir" location="src/main/java" />
    <property name="resource.dir" location="src/main/resources" />
    <property name="target.dir" location="target" />
    <property name="classes.dir" location="${target.dir}/classes" />
    <property name="lib.dir" value="lib" />
    <property name="main-class" value="com.cleaner.App" />
    <property name="spring-boot.version" value="2.3.4.RELEASE" />
    
    <target name="init">
        <mkdir dir="${build.dir}" />
    </target>

    <path id="classpath">
        <fileset dir="${basedir}/">
            <include name="${lib.dir}/*.jar" />
        </fileset>
    </path>

    <!-- Need classpath to run this -->
    <target name="compile" depends="init" description="compile the source ">
        <javac includeantruntime="false" srcdir="${src.dir}"
               destdir="${build.dir}" classpathref="classpath" />
    </target>
    
    <macrodef name="check-exejar" description="Create a spring-boot executable jar">
        <attribute name="destfile" />
        <attribute name="classes" />
        <attribute name="start-class" default="" />
        <attribute name="spring-boot-loader"/>

        <element name="resources" optional="true"
            description="includes resource collections specifying additional Java resources" />

        <element name="lib" optional="true"
            description="includes resource collections containing (jar) dependencies" />

        <sequential>
            <local name="start-class" />

            <echo>Using start class @{start-class}</echo>

            <local name="spring-boot.version" />

            <local name="destdir" />
            <dirname file="@{destfile}" property="destdir" />

            <echo>Using destination directory ${destdir}</echo>
            <mkdir dir="${destdir}/dependency" />

            <echo>Extracting spring-boot-loader to ${destdir}/dependency</echo>
            <local name="spring-boot.loader"/>
            
            <copy todir="${destdir}/dependency">
                <javaresource name="META-INF/loader/spring-boot-loader.jar">
                    <classpath location="@{spring-boot-loader}"/>
                </javaresource>
                <flattenmapper />
            </copy>

            <echo>Embedding spring-boot-loader v2.4.4...</echo>
            <jar destfile="@{destfile}" compress="false">
                <mappedresources>
                    <fileset dir="@{classes}"/>
                    <globmapper from="*" to="BOOT-INF/classes/*" />
                </mappedresources>
                <resources />
                <mappedresources>
                    <lib />
                    <globmapper from="*" to="BOOT-INF/lib/*" />
                </mappedresources>
                <zipfileset src="${destdir}/dependency/spring-boot-loader.jar" />
                <manifest>
                    <attribute name="Main-Class"
                        value="com.cleaner.App" />
                    <attribute name="Start-Class" value="org.springframework.boot.loader.PropertiesLauncher" />
                    <attribute name="Implementation-Title" value="gprd-logs-cleaner"/>
                    <attribute name="Spring-Boot-Classes" value="BOOT-INF/classes/" />
                    <attribute name="Spring-Boot-Lib" value="BOOT-INF/lib/" />
                </manifest>
            </jar>
        </sequential>
    </macrodef>
    
    <target name="build" >
        <local name="jar" />
        <property name="jar" location="${target.dir}/gprd-logs-cleaner.jar" />
        <property name="spring-boot.loader" location="${basedir}/ext-lib/spring-boot-loader-tools-2.4.4.jar" />
        <check-exejar destfile="${jar}" classes="${classes.dir}" spring-boot-loader="${spring-boot.loader}" 
            start-class="com.cleaner.App">
            <resources>
                <fileset dir="${resource.dir}" />
            </resources>
            <lib>
                <path refid="classpath" />
            </lib>
        </check-exejar>
    </target>
    
    <!-- Default, run this -->
    <target name="main" depends="build"/>

</project>

I use PropertiesLuncher because I have to extract the properties from the final jar我使用 PropertiesLuncher 因为我必须从最终的 jar 中提取属性

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

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