【springboot】部署阿里云详解(jar部署出错,jsp,freemarker路径访问不到解决办法)

前言

很早之前就完成了一个项目,但是一直没有部署到阿里云,最近要准备简历面试啦,今天花了一天时间内终于完成了springboot项目部署到阿里云。其中踩了无数坑,记录下来分享给大家。


1. 打成jar包
  1. 刚开始的时候,在控制台上使用命令:

E:\code\syau_web_1>mvn clean package -Dmaven.test.skip=true

结果jar包没打成,报了很多错
错误提示大概这样:
Could not transfer artifact org.springframework:spring-tx:jar:3.2.3.RELEASE

错误原因: 在配置maven的时候,我没有加入阿里云的镜像,导致jar文件下载不了,配置了**阿里云镜像**之后,就能够下载了。 ***
  1. 在配置完
    在打成jar包之后,运行

java -jar 名称.jar

结果访问的时候又出错了
由于我的项目里包含jsp界面,结果jsp界面访问不了,404找不到路径。

解决方法:
我查了很多资料,springboot对jsp的支持并不好,spring-boot-maven-plugin必须是1.4.2版本

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <!--版本必须是这个-->
            <version>1.4.2.RELEASE</version>
            <configuration>
                <!--使devtools能够起作用-->
                <fork>true</fork>
            </configuration>
        </plugin>

    </plugins>

  1. 在配置完之后,打成jar包,结果发现又报错了。
错误提示: [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.4.2.RELEASE:repackage (default) on project s yau_web: Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.4.2.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.example.syau_web.SyauWebApplication, com.example.syau_web.test.Even tListener, com.example.syau_web.test.Writer] -> [Help 1] 解决方法: 仔细看错误提示,有多个主类入口,所以报错了,删除掉没用的类就好啦。
  1. 在部署,又报错,数据库访问不了。

    错误提示:
    找不到数据库,大家都知道。
解决方法: 在打成jar的时候,把配置文件加进去。 ```
    <finalName>syausell</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <!--版本必须是这个-->
            <version>1.4.2.RELEASE</version>
            <configuration>
                <!--使devtools能够起作用-->
                <fork>true</fork>
            </configuration>
        </plugin>

    </plugins>

    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/**</include>
            </includes>
        </resource>

        <resource>
            <directory>src/main/webApp</directory>
            <targetPath>META-INF/resources</targetPath>
        </resource>




        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.yml</include>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>

’```


  1. 这几个错误解决了,发现我的resource下的freemarker模板访问不了了,真是惨。

    错误提示:
    找不到freemarker的访问路径
解决方法: 主要是进行jar打包的时候,把没用把freemarker放进去 把上面的提示路径该成:
<resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/**</include>
                    <!--<include>**/*.properties</include>-->
                    <!--<include>**/*.xml</include>-->
                </includes>
                <filtering>false</filtering>
            </resource>

终于能把项目打成jar包了,并且所有界面都能访问了。
整体目录结构:
在这里插入图片描述


2.发布项目到阿里云

这个挺简单的,把jar包放入阿里云linux服务器上,
让他在后台运行:

nohup java -jar -Dserver.port=8080 sell.jar &


如果要退出进程:

jobs

kill %进程编号


发布后项目项目地址:
http://www.yiyufei.top/login

总结

这次部署可真的是费劲,几乎每个坑都被我踩遍了,希望大家都能部署上自己的服务器,加油。


你为什么会近视?为了看淡世间的万物,而模糊了双眼。

猜你喜欢

转载自blog.csdn.net/qq_41346335/article/details/88560590