【问题标题】:Maven Profiles not working and failing testsMaven 配置文件不工作且测试失败
【发布时间】:2013-03-07 16:22:11
【问题描述】:

我开始在 Maven 中使用配置文件来构建多环境 jar。

我是按照official docs 来做的。

一、验证问题:

我读到你应该总是有一个由 Maven 项目生成的包,但我只想生成多环境 jar(即:只为每个 jar 更改一个属性文件)。我认为没有必要生成多个项目来执行此操作,对吗?

现在解释一下:

我有一个应用程序可以读取文件并应用一组特定评论,然后再将一些信息插入数据库。我想测试这个验证是否正常,并且无论它稍后在数据库中是否失败,我都会得到正确的结果。 所以在这个应用程序中我使用了一个动态设置的 DAO。这是:我的应用在运行时从 config.properties 文件中获取 DAO 类。我创建了一些外观 DAO,它们将模拟真实的 DAO(例如:DAOApproveAll,它将模拟数据库中的所有事务都正常)。

在单元测试中,我加载 config.properties 以更改(然后恢复更改)参数 daoimplclass 的值,该参数是包含类的参数.例如:

 Properties prop = Configurator.getProperties("config");
    final String DAODEFAULT = prop.getProperty("daoimplclass");
    final static String DAOAPPROVEALL = "com.package.dao.DAOAllApproved";
    public void testAllAproved() {
        try {
            Processor processor = Processor.getInstance();
            prop.setProperty("daoimplclass", DAOAPPROVEALL);
            ...
         }
         finally{prop.setProperty("daoimplclass", DAODEFAULT);}

我做了很多测试(使用不同的 DAO 外观),以验证如果数据库中出现不同的结果会发生什么。

现在,我将 config.properties 更改为 2 个文件:config-dev.propertiesconfig-prod.properties。并将原来的 pom.xml 更改为使用如下配置文件:

<profiles>
   <profile>
     <id>dev</id>
     <build>
       <plugins>
         <plugin>
           <artifactId>maven-antrun-plugin</artifactId>
           <executions>
             <execution>
               <phase>test</phase>
               <goals>
                 <goal>run</goal>
               </goals>
               <configuration>
                 <tasks>
                   <delete file="${project.build.outputDirectory}/config.properties"/>
                   <copy file="src/main/resources/config-dev.properties"
                         tofile="${project.build.outputDirectory}/config.properties"/>
                 </tasks>
               </configuration>
             </execution>
           </executions>
         </plugin>
         <plugin>
           <artifactId>maven-surefire-plugin</artifactId>
           <configuration>
             <skip>false</skip>
           </configuration>
         </plugin>
         <plugin>
           <artifactId>maven-jar-plugin</artifactId>
           <executions>
             <execution>
               <phase>package</phase>
               <goals>
                 <goal>jar</goal>
               </goals>
               <configuration>
                 <classifier>dev</classifier>
                 <source>1.6</source>
                 <target>1.6</target>
               </configuration>
             </execution>
           </executions>
         </plugin>
       </plugins>
     </build>
   </profile>
    <profile>
     <id>prod</id>
     <build>
       <plugins>
         <plugin>
           <artifactId>maven-antrun-plugin</artifactId>
           <executions>
             <execution>
               <phase>test</phase>
               <goals>
                 <goal>run</goal>
               </goals>
               <configuration>
                 <tasks>
                   <delete file="${project.build.outputDirectory}/config.properties"/>
                   <copy file="src/main/resources/config-prod.properties"
                         tofile="${project.build.outputDirectory}/config.properties"/>
                 </tasks>
               </configuration>
             </execution>
           </executions>
         </plugin>
         <plugin>
           <artifactId>maven-surefire-plugin</artifactId>
           <configuration>
             <skip>true</skip>
           </configuration>
         </plugin>
         <plugin>
           <artifactId>maven-jar-plugin</artifactId>
           <executions>
             <execution>
               <phase>package</phase>
               <goals>
                 <goal>jar</goal>
               </goals>
               <configuration>
                 <classifier>prod</classifier>
                 <source>1.6</source>
                 <target>1.6</target>
               </configuration>
             </execution>
           </executions>
         </plugin>
       </plugins>
     </build>
   </profile>
  </profiles>

现在,当我在 Netbeans 中执行“清理和构建”时,我在执行测试时遇到错误,因为它找不到 config.properties。当然,我创建了第三个 config.properties(另外两个将使用 -dev 和 -prod)ant,它编译但不会生成 2 个 jar,而只会生成一个。


我的正式问题:

  • 我的个人资料有什么问题?
  • 我怎样才能允许测试运行正常并且仅用于开发人员?

【问题讨论】:

    标签: java maven netbeans


    【解决方案1】:

    要通过 Netbeans 激活配置文件,您可以尝试以下任务:-

    1. 右键单击您的项目并从上下文菜单中选择properties
    2. 从左侧面板的Categories 中选择Configurations
    3. 在右侧面板中,您将看到 pom.xml 中定义的各种配置文件。您可以通过选择它们并单击Activate 按钮来Activate,或者您可以通过单击Add 按钮来创建新的。

    我希望这会有所帮助。

    【讨论】:

    • 谢谢!这就是我要找的!
    猜你喜欢
    • 2017-04-01
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 2017-05-21
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    相关资源
    最近更新 更多