I have two servers for my java application and I'm using jenkins to deploy my code on those servers. The application is same but because of the nature of work we are doing we need different versions of same custom jars on each server.
1: I've tried to set environment variables and tried to get artifact and group Id's of those in pom.xml but we can not access environment variables in pom.xml
2: I've tried changing their names and import both jars but that's insane one of them is ignored and both the servers use only one version.
I've been struggling with it for a long time now, The only possible solution that comes to my mind is that i create two different git repositories and make different jenkin jobs for every server.
Can anyone help me figure out how can I import different versions on different servers, that'd mean a lot. Thanks in advance.
If I get you correctly,
different versions of some custom jars
are different version of yours dependencies. This can be easily achieved using maven profiles. Your pom.xml would look similair to this (XML is simplified to minimum.
<project>
<!-- Basic info like model, artifact, group etc. -->
<dependencies>
<!-- Your usual deps as you are used to -->
</dependencies>
<profiles>
<profile>
<id>profile1</id>
<dependencies>
<!-- Extra deps for this profile -->
</dependencies>
</profile>
<profile>
<id>profile2</id>
<dependencies>
<!-- Extra deps for this profile -->
</dependencies>
</profile>
</profiles>
</project>
IDEs commonly provides way to set profile, so devs should not have problem . From jenkins, are while building from command line you would be invoking command with given profile. You can have separate jobs or you can create your job with parameters.
mvn install -P profile1
Alternatively, profile can be activated by enviroment variable. Problem may be that this variable must be availble during compilation.
Another aproach would be branching your code for different customers as Abhilas mentioned in comment.
Related
My application should work in 2 modes: standard and custom.
I am using the same classes but it can react little bit differently in different modes.
How better to inject this mode into all(not all but a lot) classes?
Application should be switched in run-time.
Not boot time.
I am using java8 and groovy.
Thanks!
Agree with Igor, probably spring profiles would be helpful for you. Also, I could suggest maven profiles (in case if you use maven as a build tool of course). For example, if you have two different modes "production" and "development" you can create two directories with property file props.properties and do the following with maven:
<profiles>
<profile>
<id>production</id>
<properties>
<resource.location>classpath:production</resource.location>
</properties>
</profile>
<profile>
<id>development</id>
<properties>
<resource.location>classpath:development</resource.location>
</properties>
</profile>
</profiles>
After this you are free to configure your spring property placeholer in this way
<context:property-placeholder location="${resource.location}/props.properties" ignore-unresolvable="true"/>
And build you app with
mvn install -Pdevelopment
mvn install -Pproduction
As per runtime switch, could you provide more details on your application. In case if it is a web application you could create some webservice for switching modes.
Problems:
Deployment took too long
Namespaces, configurations and contents missing in production server
What I've done:
Followed the guide to deploy my Hippo CMS project here.
What I've found:
When I extract the distribution file, I've found cms.war which includes a huge bootstrap content jar (140MB; myproject-bootstrap-content-2.01.08-SNAPSHOT).
I have a feeling that in my production server, Hippo will try to expand this jar file and write to my production MySQL server.
(That sounds a bit scary to me.)
Am I correct?
It makes the distribution file very big, right now it's 160MB we haven't even launched out site which has relatively little contents yet.
Can we configure Hippo to not include the bootstrap jar and simply connect to the MySQL database assigned?
The contents can be filled up manually.
Data entry is supposed to be a separate matter right?
you can exclude bootstrap jar by creating another profile which includes your content dependency.
Just edit cms/pom.xml and add following:
<profiles>
<profile>
<id>local</id>
<dependencies>
<dependency>
<groupId>com.YOURGROUP</groupId>
<artifactId>YOURPROJECT_ARTIFACT-bootstrap-content</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</profile>
now you can activate this by using mvn package -Plocal
By default it will not be included in distribution package (unless you add -Plocal of course)
I have one situation where I need to create multiple configuration property file by passing environment.
i.e : I have environment like dev, prod, qa, int, but I want to create jar only for int and dev in one go
My First question is: "Is there any way in maven where we can pass multiple environment value" ?
If this is possible how can I setup my pom.xml to accept the multiple env value ?
It's not quite clear what you mean with "pass multiple environment value".
If you want to activate a set of properties depending on the execution environment you could use maven profiles.
Here is an example for two profiles, which you can add to a pom.xml.
<profiles>
<profile>
<id>prod</id>
<properties>
<value1>a</value1>
<value2>b</value2>
</properties>
</profile>
<profile>
<id>qa</id>
<properties>
<value1>x</value1>
<value2>y</value2>
</properties>
</profile>
</profiles>
You could then for example activate profile "prod" by calling maven with the paramter "-Pprod".
I fear you can't trigger 2 different packaging with per-profile filtered resources in one build.
If you want to activate 2 profile at once you can specify them both using -P or trigger the activation according to the same property (see Maven: Only execute plugin when a command line flag is present)
If I correctly understand your question, i'd suggest to delegate the build of the integration artifact to a continuous integration server which will trigger the appropriate resource filtering using a dedicated profile and to set the dev profile as default one for the daily developers builds.
I use this to build my spring mvc app:
mvn clean package
I use the maven war plugin to create a war file, but the problem I am facing is that in my resources folder I have my development versions of my .properties files for log4j etc.
When I push to production, and run:
java -jar ...
It explodeds the war file, and then at that point I can modify the .properties files with my production settings, but I obviously want to do this during my maven build for production.
Is there a way I can tell maven that this is a production build, so get these files from somewhere else? And during development, keep doing what it is doing now?
User maven profiles. Maven profiles help you in specifying different properties for different profiles. So you can have two profiles - development and production.
Something like this -
<profiles>
<profile>
<id>development</id>
<!-- we'll properties here... -->
</profile>
<profile>
<id>production</id>
<!-- ...and here -->
</profile>
</profiles>
Like this example -
<profile>
<id>development</id>
<properties>
<db.driverClass>oracle.jdbc.driver.OracleDriver</db.driverClass>
<db.connectionURL>jdbc:oracle:thin:#127.0.0.1:1521:XE</db.connectionURL>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<db.driverClass>oracle.jdbc.driver.OracleDriver</db.driverClass>
<db.connectionURL>jdbc:oracle:thin:#134.0.0.1:3124:XE</db.connectionURL>
</properties>
</profile>
There are a couple of options here. The first (as others have mentioned as well) is to use maven profiles. Instead of having multiple version of your properties files, you would have something like:
mypropsfile.properties
-----------------------
prop1=${prop1.val}
prop2=${prop2.val}
Then in your profiles, you can define values for those properties (make sure you have resource filtering enabled for this to work. see an example http://www.manydesigns.com/en/portofino/portofino3/tutorials/using-maven-profiles-and-resource-filtering).
You can also have your properties file have production values in it but with the ability to override those files in development. Spring profiles are helpful for this. For example, in development mode you can look for a properties file named <user-home>/mypropertiesoverride.properties, which could be used to override any of the production value properties with development specific ones.
I prefer the second method here where you have a default properties file and then you can just override select properties.
Is there a way to change the version number without editing the POM?
<groupId>org.example</groupId>
<artifactId>example</artifactId>
<version>1.0.0</version>
We have a CI system where we want to release nightly builds, but without using the -SNAPSHOT solution of Maven, so if 1.0.0 is the current version, we just want to have CI-NIGHTLY-BIULD-20120426.
I suggested this would be possible with something like mvn deploy -Dversion=CI-NIGHTLY-BIULD-20120426, but obviously not. The bad solution would be to let the CI server edit the pom.xml every time, but I think this is very unhandy.
Thank you!
I suggest to use classifier.
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>1.0</version>
<properties>
<!-- default classifier is empty -->
<my.project.classifier></my.project.classifier>
</properties>
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<classifier>${my.project.classifier}</classifier>
</configuration>
<executions>...</executions>
</plugin>
</plugins>
</build>
and
mvn package -Dmy.project.classifier=NIGHTLY-2012-04-26_02-30
Maven documentation says about classifier:
classifier: You may occasionally find a fifth element on the
coordinate, and that is the classifier. We will visit the classifier
later, but for now it suffices to know that those kinds of projects
are displayed as groupId:artifactId:packaging:classifier:version.
and
The classifier allows to distinguish artifacts that were built from
the same POM but differ in their content. It is some optional and
arbitrary string that - if present - is appended to the artifact name
just after the version number. As a motivation for this element,
consider for example a project that offers an artifact targeting JRE
1.5 but at the same time also an artifact that still supports JRE 1.4. The first artifact could be equipped with the classifier jdk15 and the
second one with jdk14 such that clients can choose which one to use.
Another common use case for classifiers is the need to attach
secondary artifacts to the project's main artifact. If you browse the
Maven central repository, you will notice that the classifiers sources
and javadoc are used to deploy the project source code and API docs
along with the packaged class files.
I think you could also use versions maven plugin. I find it quite useful for things like this.
You could do it in 2 steps:
set necessary version: mvn versions:set -DnewVersion=CI-NIGHTLY-BIULD-20120426
deploy: mvn deploy
in case you need to revert back the changes, use mvn versions:revert (as Mark suggests)
I highly recommend reading Maven Releases on Steroids (part 2, part 3) by Axel Fontaine. It is great, and I'm quite happy using it.
It not only details how you con do what you ask, but also contains good advice how you can tie your build versions with your CI server.
In a nutshell, here are the main points:
Maven Release is slow, needs to be done faster
You parametarize your project version like
<version>${VERSION_NUMBER}</version>
...
<properties>
...
<VERSION_NUMBER>1.0-SNAPSHOT</VERSION_NUMBER>
...
</properties>
Local builds get that version: 1.0-SNAPSHOT
Release builds are done only from your CI server
In your Jenkins/Hudson project configuration you use
clean deploy scm:tag -DVERSION_NUMBER=${BUILD_NUMBER}
That way you get a new release with each Jenkins build, not only nightly.
You can change the configuration to use
clean deploy scm:tag -DVERSION_NUMBER=1.0.0-CI-NIGHTLY-BIULD-${BUILD_ID}
and you would get versions like 1.0.0-CI-NIGHTLY-BIULD-2012-04-26_12-20-24
You could parameterize the version number as
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>${my.project.version}</version>
<properties>
<my.project.version>1.0</my.project.version>
</properties>
and drive the version number from command line using
mvn package -Dmy.project.version=NIGHTLY
Although this is possible, Maven 3 discourages it.