我正在尝试将我的资源复制到类路径中,具体取决于使用maven选择的配置文件。我的资源文件夹结构如下:
src/main/resources:
config
production
development
staging
我当前的无效配置是
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>envtype</name>
<value>dev</value>
</property>
</activation>
<build>
<finalName>Corelay</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<excludes>
<exclude>**/production/**</exclude>
<exclude>**/staging/**</exclude>
</excludes>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<excludes>
<exclude>**/production/**</exclude>
<exclude>**/staging/**</exclude>
</excludes>
</testResource>
</testResources>
</build>
</profile>
在config / hibernate / hibernate-config.xml下的hibernate配置文件中,我请求来自同一个包的一些属性
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:**/jdbc.properties</value>
<value>classpath*:**/hibernate.properties</value>
</list>
</property>
</bean>
但是有一个错误:
Could not resolve placeholder 'jdbc.driverClassName' in string value "${jdbc.driverClassName}"
此属性在该文件中定义。怎么了?另一个问题是如何使从这些配置文件夹复制的资源出现在完全相同的输出类路径结构中?我的意思是应该没有/ production,/ development或/ staging:just / env
我知道我可以把它们分开但是如果有共享的那些(比如所提出的结构中的配置)我怎么能包括它呢?
答案 0 :(得分:1)
在与src / main / resources相同的级别创建一个文件夹src / main / config。在里面创建3个子文件夹common,dev和production:
| _ _common
| | _ _common.properties
| _ _dev
| | _ _dev.properties
| _ _prod
| | _ _prod.properties
然后配置两个配置文件,开发和生产:
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/config/common</directory>
</resource>
<resource>
<directory>src/main/config/dev</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<id>prod</id>
<build>
<resources>
<resource>
<directory>src/main/config/common</directory>
</resource>
<resource>
<directory>src/main/config/prod</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
这样,mvn clean install将common.properties和dev.properties复制到类路径的根目录,因为默认情况下dev开发者是活动的。
mvn clean install -Pprod然后会安装common.properties和production.properties,但是没有dev.properties,也会安装到classpath的根目录。