Access a Java variable from Jenkins - java

I am trying to find out if I could get the value of a variable declared inside a Java program or maven-plugin and store it in a Jenkins environment variable.
This is because the Jenkins file has to create a new git branch from dev and call it Release-9.0.86 for example but the version number is inside a pom.xml.
I have already written a maven-plugin that retrieves the version from the pom.xml an writes it into another XML file, but I need to know if I could send the version to Jenkins to handle it.

You could perhaps create a properties file instead of a xml file with the needed information. Example contents of created branch-name.properties file:
NEW_BRANCH_NAME=Release-9.0.86
Then you can add a build step "Inject environment variables" after the one that produces the properties file, and configure that to read the properties file you just created.
Properties File Path: branch-name.properties
After that you can use the environment variables as usual in the jenkins build, for example a "Execute shell" build step that creates the branch:
git branch ${NEW_BRANCH_NAME}
To then push the newly created branch you could add a "Git Publisher" post-build action.
Branch to push: ${NEW_BRANCH_NAME}
Please note that it might be useful to configure Git Publisher to "Push Only If Build Succeeds" i.e. branch will not be created if the build fails for any reason.

Related

Cassandra: Error opening zip file or JAR manifest missing : D:\cassandra\lib\jamm-0.3.2

Been trying to build Apache Cassandra using Ant. The build is successful, but the Cassandra server won't start using bin\cassandra because of the error: Error opening zip file or JAR manifest missing : D:\cassandra\lib\jamm-0.3.2.
Here is the environment setup:
JDK 8 bin directory added to PATH
JAVA_HOME is set to the JDK 8 directory
CASSANDRA_HOME is set to D:\cassandra, where I cloned the repo
D:\cassandra\lib\jamm-0.3.2.jar exists
Just in case I tried recloning the repo in another directory, rebuilding, but same error.
Checking the output when building, I noticed warnings like this one, I don't know if it is relevant.
[artifact:dependencies] [WARNING] POM for 'org.perfkit.sjk.parsers:sjk-jfr5:pom:
0.5:compile' is invalid.
[artifact:dependencies]
[artifact:dependencies] Its dependencies (if any) will NOT be available to the c
urrent build.
Any idea of what else I could test? Thank you.
I've seen this issue, too, when running Cassandra from my build directory.
What you'll want to do is to "deploy" your build (target) to a different directory, and set your CASSANDRA_HOME to that. Essentially, you'll want to emulate the how the "delivered" directory structure looks, ensuring that subdirs like bin/, conf/ and lib/ are at the same directory level.
Try that and see if it helps.
I forgot to mention that I set the environment variables PATH, CASSANDRA_HOME and JAVA_HOME using the set command.
By going to the system parameters and changing the user variables, it worked. I changed those three at the same time so I am not sure which one was blocking.

Git manage environment specific configuration

I have a requirement to have a property configuration for different environment like dev, uat and production. For example a config.properties having and entry like environment=dev, this I need to change for staging branch as environment=uat and for master branch as environment=prd .
I tried to commit these files in each branch respectively and tried adding config.properties in gitignore so that it will not consider in next commits.
But git ignore not getting updated so I ran command
git rm -rf --cached src/config.properties
git add src/config.properties
git commit -m ".gitignore fix"
But this command is deleting the file from local repository itself and the proceeding commits also deleting from branches. I want to handle the branch as such so as Jenkins will do the deployment without editing config file manually. I am using fork for git UI. Is there any way to handle this kind of situation?
You should not version a config.properties (git rm is right), and ignore it indeed.
That way, it won't pose any issue during merge.
It is easier to have three separate files, one per environment:
config.properties.dev
config.properties.uat
config.properties.prd
In each branch, you would then generate config.properties, with the right value in it, from one of those files, depending on the current execution environment.
Since you have separate branches per environment, with the right file in it, you can have a generation script which will determine the name of the checked out branch with:
branch=$(git rev-parse --symbolic --abbrev-ref HEAD)
That means you could:
version only a template file config.properties.<env>
version value files named after the branches: config.properties.dev, config.properties.uat...: since they are different, there is no merge issue when merging or switching branches.
Finally, you would register (in a .gitattributes declaration) a content filter driver.
(image from "Customizing Git - Git Attributes", from "Pro Git book")
The smudge script, associated to the template file (package.json.tpl), would generate (automatically, on git checkout) the actual config.properties file by looking values in the right config.properties.<env> value file.
The generated actual config.properties file remains ignored (by the .gitignore).
See a complete example at "git smudge/clean filter between branches".

Using System Path Variable in Maven project - Eclipse

PROBLEM: Hello everyone! I have created a Maven project in which i am building
a RESTful API, now i have created few .json files in which i want
to store my JSON data in src/main/resources , but as i can see
every time i make a change ( example: register a user - write in a
file ) all changes get lost whenever i redeploy the WAR file or the
server gets restarted/reloaded.
Can someone please tell me how to use system PATH variable to fix
my problem? I want to store files somewhere i will be able to update
them without having to worry if server is being reloaded or not. Thank you in advance!
SMART WAY, with java configuration and classpath (unix example, launch your application this way)
java -classpath $PATH <your-class-name>
or
setenv CLASSPATH = $PATH
see unix and windows instructions.
BAD WAY, within application code
You can read the PATH variable from the OS environment calling System.getenv() and then looking for the "PATH" elements within the returned map.
Then you have to parse the string (e.g. with StringTokenizer) obtained to get each folder within the PATH variable
you have to look for your JSON files in each of the folder from previous point

Heroku multi buildpack with nginx, java and node

I have a node app which contains a node plugin that references a jar file. My question is this -
Is there a specific example of a heroku multi buildpack which loads all three -
...nginx.git
...java.git
...nodejs.git
In my root I have the following:
.buildpacks
.nginx
system.properties
package.json
Also, if this is a native nodejs app that is dependant upon a plugin which refrences a jar file and hence need to load the java environment, is it necessary that I also include a POM.XML file in my application's root?
Instead of the Java buildpack, use the jvm-common buildpack. This way, you won't need a pom.xml. You can put this line in your .buildpacks file:
https://github.com/heroku/heroku-buildpack-jvm-common
Or set it with the heroku buildpacks command.

Maven dependency plugin problems

I use maven-assembly plugin to create a zip file for a release in my target folder. This package with dynamic name includes a configuration file;
/target/dailyrelease-4234.zip/cd/lib/conf/wrapper.conf
Now I also use maven-dependency plugin's build-classpath goal to output the dependencies as a string.
I want to write this output string to the configuration file created by the assembly plugin
I have 2 problems:
1- How can I access this conf file in the dynamic named zip?
2- I want to add some extra .jar paths to that string created by maven dependency plugin, but it only copies the names from local repository. is there a way to modify this output, or show dependency plugin to use another folder to pick the jar names and not from local repository?
Or even better make the creating dependency names task as a part of assembly-plugin so I dont need to access and modify that zip anymore.
1 - It sounds like your mechanism for dynamically generating that number '4234' exists outside of Maven, and you're trying to figure out how to access that number from within Maven, correct?
If so, I suggest using the buildnumber-maven-plugin which generates a number which you can then access from within Maven via the ${buildNumber} property.
2 - I suggest switching your callout from dependency:build-classpath to dependency:list -DoutputFile=xyz.txt. The latter gives you a cleaner output of just groupId/artifactId/version which should be easy to edit.
Or better yet... Do the above, and simply use <phase> configuration to ensure dependency:list gets called before the assembly plugin runs (typically at the end of package phase), and ensure the resulting output file sits somewhere that the assembly plugin will pick it up.
Hope that helps.

Categories