3

settings.xmlプロファイルパラメータをJavaクラスに挿入したいと思います。maven-annotation-pluginを使用しようとしましたが、値がnullになりました。このプラグインはMojo用に設計されているからだろうか

Setting.xmlスニペット

  <profiles>
    <profile>
      <id>APP_NAME</id>
      <properties>
        <test.email>USER_EMAIL</test.email>
        <test.password>USER_PASSWORD</test.password>
      </properties>
    </profile>
  </profiles>

クラスで

@Parameter(defaultValue = "test.email", readonly = true)
private String userEmail;

@Parameter(defaultValue = "test.password", readonly = true)
private String userPassword;
4

2 に答える 2

7

私はファイルmaven-resources-pluginを生成し、.propertiesコードの生成を回避するために使用します。

<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
<build>

そしてファイルを作成しますsrc/main/resources/com/example/your/file.properties

testMail = ${test.email}
propertyName = ${maven.variable.name}

Javaでは、次のコマンドでアクセスします。

getClass().getResourceAsStream("/com/example/your/file.properties")

さらに進むには、次のコマンドでtest.emailプロパティの存在を強制できmaven-enforcer-pluginます。

<build>
  <plugin>
    <artifactId>maven-enforcer-plugin</artifactId>
    <executions>
      <execution>
        <id>enforce-email-properties</id>
        <goals>
          <goal>enforce</goal>
        </goals>
        <configuration>
          <rules>
            <requireProperty>
              <property>test.email</property>
              <message>
                The 'test.email' property is missing.
                It must [your free error text here]
              </message>
            </requireProperty>
          </rules>
        </configuration>
      </execution>
    </executions>
  </plugin>
</build>
于 2013-03-18T11:20:12.703 に答える
1

Mavenを使用してPropertiesファイルを生成し、 それをJavaクラスにロードすることができます。

于 2013-03-18T11:20:49.947 に答える