maven

听说maven挺好使,不止我之前认为的jar包管理工具,对于外部文件配置什么的也有(自己真是水的一比),那就学学吧

那就先建个外部文件吧

在这个位置,里头代码是:

book.author=www罗贯中
book.name=三国演义
book.pinyin=sanguoyanyi

建个类读一下吧,

package com.example.demo.test;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.Properties;

@RestController
@RequestMapping("/test2")
public class Test2 {

    @RequestMapping("/read")
    public String qu() {
        try {
            Properties properties = new Properties();
            InputStream input =
                    new BufferedInputStream(new FileInputStream("conf/conf.properties"));
            properties.load(new InputStreamReader(input, "UTF-8"));
            String author = properties.getProperty("book.author");
            String name = properties.getProperty("book.name");
            String pinyin = properties.getProperty("book.pinyin");
            System.out.println(author + name + pinyin);
            return author + name + pinyin;
        } catch (IOException e) {
            e.printStackTrace();
            return "异常原因:"+e.getMessage();
        }
    }
}

必须提一下,这中间还碰到了io流读外部文件输出乱码的情况,就这样解决吧先,

好的,那就按照之前的套路打个包吧,打好了,运行:

好的,错误出来了,那就看看日志吧:

MMP,为何日志中没有,又打断我思路,经过一番搜索,不捕捉异常了,throw吧,

package com.example.demo.test;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.util.Properties;

@RestController
@RequestMapping("/test2")
public class Test2 {

    @RequestMapping("/read")
    public String qu() throws IOException {
        Properties properties = new Properties();
        InputStream input =
                new BufferedInputStream(new FileInputStream("conf/conf.properties"));
        properties.load(new InputStreamReader(input, "UTF-8"));
        String author = properties.getProperty("book.author");
        String name = properties.getProperty("book.name");
        String pinyin = properties.getProperty("book.pinyin");
        System.out.println(author + name + pinyin);
        return author + name + pinyin;
//        try {
//        } catch (IOException e) {
//            e.printStackTrace();
//            return "异常原因:"+e.getMessage();
//        }
    }
}

就这样,log文件中异常信息也有了,还是步入正题吧,

改下pom,编译一下,结构如下:

好的,失败了,再找找。。。。

等等,我的目的是什么呢?我现在是要打包把外部文件也打进去吧?

那这样的话,貌似我刚才那包没打错,也只是没把外部jar和properties打进去而已,那我要不试试能启动不,把配置文件放文件夹中,lib中放jar包,pom中添加:

<build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <archive>
                            <!-- 不打包依赖的jar,把依赖的jar copy到lib目录,和生成的jar放在同一级目录下 -->
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <classpathPrefix>lib/</classpathPrefix>
                                <mainClass>com.example.demo.TestaopApplication</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

包结构这么个情况,(logs是输出日志,别管了)

我擦,启动调用都成功了,尴尬不,不过千万别忘了自己想干嘛,我想把这些都打到一起去啊,接着搞吧

明天再说吧。。。。。。。。

我目的就是为了把配置文件打进去,好了,

<resource>
    <directory>conf</directory>
    <includes>
        <include>**/*.properties</include>
        <include>**/*.xml</include>
    </includes>
</resource>

但发现打包后conf文件夹消失,报了找不到文件异常,那就改改吧

            <resource>
                <directory>conf</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <!-- 指定打包到目的文件夹 -->
                <targetPath>conf</targetPath>
            </resource>

指定文件夹,ok了,打包启动又是找不到文件,我去啊,什么鬼,改代码,换读取文件代码,

【我们不能用常规操作文件的方法来读取ResourceJar.jar中的资源文件res.txt,但可以通过Class类的getResourceAsStream()方法来获取 ,这种方法是如何读取jar中的资源文件的,这一点对于我们来说是透明的。】

@RequestMapping("/read")
    public String qu() throws IOException {
        Properties properties = new Properties();
        InputStream input = this.getClass().getResourceAsStream("/conf/conf.properties");
                //new BufferedInputStream(new FileInputStream("conf/conf.properties"));
        properties.load(new InputStreamReader(input, "UTF-8"));
        String author = properties.getProperty("book.author");
        String name = properties.getProperty("book.name");
        String pinyin = properties.getProperty("book.pinyin");
        System.out.println(author + name + pinyin);
        return author + name + pinyin;
//        try {
//        } catch (IOException e) {
//            e.printStackTrace();
//            return "异常原因:"+e.getMessage();
//        }
    }

打包,启动运行,终于他娘的可以了

 

###后续,想看看springboot下maven多环境打包,过程挺坎坷的,不说了,直接上代码

首先是项目配置文件结构:

application.properties:

spring.profiles.active=@profileActive@
spring.http.multipart.maxFileSize=100Mb
spring.http.multipart.maxRequestSize=100Mb

application-dev.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/wuxiaokai
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456

application-oracle.properties:

spring.datasource.url=jdbc:oracle:thin:@10.135.102.102:1521:tkwmss
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.username=wserver
spring.datasource.password=wserver

pom.xml:

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault><!--此处将dev设置为默认环境-->
            </activation>
            <properties>
                <profileActive>dev</profileActive>
            </properties>
        </profile>
        <profile>
            <id>oracle</id>
            <properties>
                <profileActive>oracle</profileActive>
            </properties>
        </profile>
    </profiles>


    <build>
        <!--使用指定的filter进行过滤-->
        <filters>
            <filter>src/main/resources/application-${profileActive}.properties</filter>
        </filters>
        <finalName>${artifactId}-${profileActive}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>conf</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <!-- 指定打包到目的文件夹 -->
                <targetPath>conf</targetPath>
            </resource>
        </resources>
    </build>

依照不同命令即可打包:

可以了,先这样吧。

 

###

可以使用 mvn package -Pdev打包出在开发环境下运行的文件,如果你想打包测试环境,生产环境,只需要修改参数 -Ptest -Pprod。 
注意:此处 -P为大写,后面参数为上面步骤配置的id。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值