将Maven参数注入Java类
我想将settings.xml概要文件参数注入Java类。我尝试使用maven注释插件,但值为空。我想知道这是否是因为这个插件是为Mojo设计的 Setting.xml代码段将Maven参数注入Java类,java,maven,code-injection,Java,Maven,Code Injection,我想将settings.xml概要文件参数注入Java类。我尝试使用maven注释插件,但值为空。我想知道这是否是因为这个插件是为Mojo设计的 Setting.xml代码段 应用程序名称 用户电子邮件 用户密码 课堂上 @Parameter(defaultValue = "test.email", readonly = true) private String userEmail; @Parameter(defaultValue = "test.password", readonly =
应用程序名称
用户电子邮件
用户密码
课堂上
@Parameter(defaultValue = "test.email", readonly = true)
private String userEmail;
@Parameter(defaultValue = "test.password", readonly = true)
private String userPassword;
我会使用
maven资源插件
生成.properties
文件,避免生成代码
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<build>
在Java中,通过以下方式访问它:
getClass().getResourceAsStream("/com/example/your/file.properties")
更进一步,您可以使用maven enforcer plugin
强制显示test.email
属性:
<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>
maven enforcer插件
强制电子邮件属性
执行
test.email
缺少“test.email”属性。
它必须[此处为您的自由错误文本]
您可以使用Maven在Java类中生成一个文件和它。我也考虑过这一点,但总有人为因素,一些开发人员/贡献者将其私人凭证提交给repo。我认为settings.xml是更安全的解决方案。我完成了回答,正如您所看到的,可以在settings.xml
或命令行中设置凭据。@shellholic是否可以将属性值提取到java类中。事实上,我知道我们可以读取属性文件并获得java中的所有值。但我想要一些像可变注射的东西。或者,请告诉我是否有任何新的库可以帮助我在运行时将变量自动注入java类。
<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>