GithubHelp home page GithubHelp logo

yaohwu / notes Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 2.35 MB

๐Ÿ““ notes

Home Page: http://notes.yaohwu.xyz/

CSS 3.55% JavaScript 22.19% SCSS 47.09% EJS 27.17%
hexo-blog personal-website share

notes's Introduction

notes's People

Contributors

yaohwu avatar

Stargazers

 avatar

Watchers

 avatar

notes's Issues

how to make a executable jar for spring boot application

This is the docs for my project spring boot version
https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/

This is the issue about this problem
spring-projects/spring-boot#4375

I want using maven shade plugin jar first cause the final jar will be smaller I think.
Even though I cannot using java -jar , java -cp "lib.jar" Main is ok for me.

I tried this pom.xml config.

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.4</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <artifactSet>
                        <excludes>
                            <exclude>com.fr.third:fine-third</exclude>
                        </excludes>
                    </artifactSet>
                    <shadedArtifactAttached>true</shadedArtifactAttached>
                    <shadedClassifierName>cloud-swift-fatty</shadedClassifierName>
                </configuration>
            </execution>
        </executions>
    </plugin>

But the final jar can not work when I using java -cp "lib.jar" Main.
Here is the exception.

java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.
     at org.springframework.util.Assert.notEmpty(Assert.java:464) ~[swift-spring-boot-2021.11-FEATURE-SNAPSHOT-cloud-swift-fatty.jar:na]
     at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.getCandidateConfigurations(AutoConfigurationImportSelector.java:183) ~[swift-spring-boot-2021.11-FEATURE-SNAPSHOT-cloud-swift-fatty.jar:na]
......

I tried spring boot mvn plugin using this pom.xml config.

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.1.3.RELEASE</version>
    <configuration>
        <fork>true</fork>
        <mainClass>${start-class}</mainClass>
        <excludes>
            <exclude>
                <groupId>com.fr.third</groupId>
                <artifactId>fine-third</artifactId>
            </exclude>
        </excludes>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

While here is the final jar format.

example.jar
|
+-META-INF
|  +-MANIFEST.MF
+-org
|  +-springframework
|     +-boot
|        +-loader
|           +-<spring boot loader classes>
+-BOOT-INF
    +-classes
    |  +-mycompany
    |     +-project
    |        +-YourClasses.class
    +-lib
       +-dependency1.jar
       +-dependency2.jar

You can find docs here
https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html

And the current module will be compiled into BOOT-INF/classes, but the other modules into BOOT-INF/lib.
Thing change worse.
spring-projects/spring-boot#4375

I found some module class cannot be loaded by Classloader.loadClass().

As so far, what should I do is using both of them, maven shade plugin and spring boot maven plugin.

Here is the config of pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.4</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <artifactSet>
                    <excludes>
                        <exclude>com.fr.third:fine-third</exclude>
                    </excludes>
                </artifactSet>
                <!--https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/2.1.3.RELEASE/spring-boot-starter-parent-2.1.3.RELEASE.pom-->
                <transformers>
                    <transformer
                            implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.handlers</resource>
                    </transformer>
                    <transformer
                            implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
                        <resource>META-INF/spring.factories</resource>
                    </transformer>
                    <transformer
                            implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.schemas</resource>
                    </transformer>
                    <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                    <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>${start-class}</mainClass>
                    </transformer>
                </transformers>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <shadedClassifierName>cloud-swift-fatty</shadedClassifierName>
            </configuration>
        </execution>
    </executions>
</plugin>

But I find some controllers cannot be scanned by spring cause the SpringBootApplication

@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
// ......
}

maven shade plugin may not create correct auto configuration for spring boot.
I have to change the code.
Update:
Controller not found is caused by my spring boot module not shade other module jars which contain other controller.
No need to change code.
I change spring boot maven plugin config to app module from spring boot module, and then it works.

@SpringBootApplication(scanBasePackages = "com.fr.swift.cloud")

Finally, all is ok.
At least for now.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.