简体   繁体   English

使用Maven构建2个不同来源的jar

[英]Building 2 jars with different source with Maven

I am trying to build 2 jars from 1 java project. 我正在尝试从1个Java项目构建2个jar。 The 2 jars would have the exact same source EXCEPT at build time one would build with a boolean variable set to True and one would build with that same boolean variable set to False. 这两个jar会在构建时具有完全相同的源,除了一个会在布尔值设置为True的情况下构建,而另一个在布尔值设置为False的情况下构建。

Example: 例:

Jar 1 would have all the same source except in 1 java file it would have: Jar 1将具有所有相同的源,但在1个Java文件中将具有:

public static final boolean ENABLE_TOAST = True;

Jar 2 would have all the same source except in that same java file it would have: Jar 2将具有所有相同的源,但在同一java文件中将具有:

public static final boolean ENABLE_TOAST = False;

I am relatively new to Maven and Ant and I had the idea of using the maven-replacer-plugin. 我对Maven和Ant相对较新,因此有了使用maven-replacer-plugin的想法。 However, I cannot find anyone who has done something like this before. 但是,我找不到以前做过这样的事情的人。

My approach is to create three properties files: 我的方法是创建三个属性文件:

  1. app.properties app.properties
  2. app.properties.test.environment app.properties.test.environment
  3. app.properties.prod.environment app.properties.prod.environment

2) and 3) have the different settings for each environment (ie ENABLE_TOAST = false for prod and true for test) 2)和3)对于每种环境都有不同的设置(即ENABLE_TOAST = prod为false,测试为true)

The application, of course, only uses app.properties 该应用程序当然仅使用app.properties

At build time, I replace app.properties with the contents of the correct environment (2 or 3) 在构建时,我将app.properties替换为正确环境(2或3)的内容

In ANT, I have two targets each with a copy file command with an overwrite flag set to true. 在ANT中,我有两个目标,每个目标都有一个复制文件命令,且覆盖标志设置为true。

I'm sure MAVEN has a similar feature (too busy to look it up ATM) 我确定MAVEN具有类似的功能(太忙了,无法在ATM上查找它)

hope this helps 希望这可以帮助

What you are trying to do is not an standard practice and recommended also, you should make your code free from these types of practices. 您尝试执行的操作不是标准做法,因此也不建议这样做,您应该使代码脱离这些类型的做法。

Best solution for these types of problems is externalize as many as property we can and most importantly try to avoid to make these properties file as part of jar. 此类问题的最佳解决方案是尽可能多地外部化属性,最重要的是,尽量避免使这些属性文件成为jar的一部分。 Store them somewhere in your server. 将它们存储在服务器中的某个位置。

I can give you a very simple example , in our application we have different database connection details for different environment like Dev , QA , UAT etc , so there instead of integrating all these things in the java code , we provided all those values in the properties file and with the help of java code we are reading those properties during application startup time. 我可以举一个非常简单的例子,在我们的应用程序中,针对不同的环境(例如Dev,QA,UAT等),我们具有不同的数据库连接详细信息,因此,我们在属性中提供了所有这些值,而不是将所有这些东西集成到Java代码中文件,借助Java代码,我们正在应用程序启动期间读取这些属性。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM