Reputation: 5177
In my android project i am using MAVEN for staging and production builds. The only difference between these two is that i use different URL for consuming the rest services. So i am looking for a way in which i can use MAVEN to make both builds. I am thinking of moving the rest URL portion of my code to a separate java jar and declare this jar as a dependency in my project.
So i will have two jar. One that will point my application to production and one that will point my application to a staging URL. Now i would need to include only one of these based on a parameter when i say something like mvn clean install -staging or mvn clean isntall -production
How can i achieve this. Any help in this will be appreciated.
Upvotes: 0
Views: 2236
Reputation: 2358
Have a look at Maven Profiles. Profiles, when active, modify specific areas of your pom - so you could include or exclude a dependency by modifying the <dependencies>
element, for example. You can specify which profile to use at build time easily on the command line like this:
mvn package -Pstaging
So for example, if you wanted to use you could add some profiles to your pom like this:
<project>
...
<profiles>
<profile>
<name>staging</name>
<properties>
<rest.url>http://staging.rest/url</rest.url>
</properties>
</profile>
<profile>
<name>production</name>
<properties>
<rest.url>http://production.rest/url</rest.url>
</properties>
</profile>
<profiles>
</project>
Then when you're ready to go, you can run your build using the appropriate profile:
mvn clean install -Pstaging
The maven property will be set by the profile. You can use this property elsewhere in your pom, for example you might filter a properties file to include the rest.url
value, and have your application read it at runtime. Have a look at the Maven Resources Plugin, which allows you to define resources that will be used by the application, and filter them based on properties in your pom.
Upvotes: 1
Reputation: 49341
I would not recommend to use profiles to change the content of your build artifact. Once your artifact is build and deployed to your repository you are not able to distinguish if it was build for your staging or production environment (unless you look inside the artifact).
So as a simple solution I would make a second pom.xml
for staging (e.g. pom-staging.xml
) and give the artifact dedicated Maven coordinates like,
<groupId>foo.bar.staging</groupId>
<artifactId>my-app</artifactId>
You can than change everything that is different for the staging environment in this pom (e.g. dependencies) and build a dedicated statging application with
mvn -f pom-staging.xml install
Upvotes: 1