Maven
关于Maven项目build时出现No compiler is provided in this environment的处理
https://blog.csdn.net/lslk9898/article/details/73836745
-
子继承父
-
scope
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
-
exclusions
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
<!-- 排除依赖 -->
<exclusions>
<exclusion>
<artifactId>javassist</artifactId>
<groupId>javassist</groupId>
</exclusion>
</exclusions>
</dependency>
-
谁在前用谁的
-
用路径少的
-
dependencyManagement
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
</dependencies>
</dependencyManagement>
-
定义版本常量
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
<struts.version>2.3.24</struts.version>
<hibernate.version>5.0.7.Final</hibernate.version>
</properties>
什么是MavenProfile
在我们平常的java开发中,会经常使用到很多配制文件(xxx.properties,xxx.xml),而当我们在本地开发(dev),测试环境测试(test),线上生产使用(product)时,需要不停的去修改这些配制文件,次数一多,相当麻烦。现在,利用maven的filter和profile功能,我们可实现在编译阶段简单的指定一个参数就能切换配制,提高效率,还不容易出错.
profile可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。
maven变量使用进行MavenProfile配置
<!--定义一个port变量,运行时默认加载这个变量-->
<properties>
<port>9105</port>
</properties>
<!--定义配置文件 id为配置的分组-->
<profiles>
<profile>
<id>dev</id>
<properties>
<port>9105</port>
</properties>
</profile>
<profile>
<id>pro</id>
<properties>
<port>9205</port>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 指定端口 -->
<port>${port}</port>
<!-- 请求路径 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
执行命令 tomcat7:run -P pro 发现以9205端口启动
执行命令 tomcat7:run -P dev 发现以9105端口启动
-P 后边跟的是profile的id
如果我们只执行命令tomcat7:run ,也是以9105启动,因为我们一开始定义的变量值就是9105,就是在不指定profileID时的默认值.
maven 资源过滤与变量替换配置MavenProfile
1.资源文件创建 filter文件夹
创建db_dev.properties用于配置开发环境用到的数据库
env.jdbc.driver=com.mysql.jdbc.Driver
env.jdbc.url=jdbc:mysql://localhost:3306/pinyougoudb?characterEncoding=utf-8
env.jdbc.username=root
env.jdbc.password=123456
filter文件夹下创建db_pro.properties
env.jdbc.driver=com.mysql.jdbc.Driver
env.jdbc.url=jdbc:mysql://localhost:3306/pinyougoudb_pro?characterEncoding=utf-8
env.jdbc.username=root
env.jdbc.password=123456
修改properties下的db.properties。spring连接数据库的配置文件使用的是jdbc.driver进行配置的,也可以不适用直接使用${env.jdbc.driver}这样进行配置,没有进行尝试
jdbc.driver=${env.jdbc.driver}
jdbc.url=${env.jdbc.url}
jdbc.username=${env.jdbc.username}
jdbc.password=${env.jdbc.password}
修改pom.xml
<!--定义一个port变量,运行时默认加载这个变量-->
<properties>
<env>dev</env>
</properties>
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
</profile>
<profile>
<id>pro</id>
<properties>
<env>pro</env>
</properties>
</profile>
</profiles>
修改pom.xml ,在build节点中添加如下配置
<filters>
<filter>src/main/resources/filters/db_${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
如果没有filter不用进行 <filters>过滤
在pinyougou-dao 工程 执行命令:package -P pro , 解压生成的jar包,观察db.properties配置文件内容,已经替换为生产环境的值。