How to get current Git branch name in Eclipse run configurations - java

I am running Eclipse with Maven, eGit and TestNG plugins, with my tests running against SauceLabs.
I would like to get the project's current Git branch name in the Maven run configuration so my code can see it at runtime and name the SauceLabs session appropriately.
A typical current Maven goals line in a run configuration looks like this:
-DtestNG.suiteXmlFile="config/regress_saucelabs.xml" clean -X test
I would like to be able to write something like the following instead:
-DtestNG.suiteXmlFile="config/regress_saucelabs.xml" -Dgit.branch=$ECLIPSE_GIT_BRANCH clean -X test
where ECLIPSE_GIT_BRANCH is an Eclipse variable that gets substituted when the run configuration is used.
Runtime solutions are a last resort, because the test code needs to also run in environments where the source repository is not available.
Strongly prefer injecting the branch name at build time, on the Maven command line. Getting it as a POM variable would work well too.

I have never use it but it looks like JGit is what you are looking for (https://doc.nuxeo.com/blog/jgit-example/)
Another solution is to call the shell command git branch in Java and then parse the result.
Another solution using maven plugin is explained here : Put current git branch to project version

Related

How to run maven build multithreaded all the time by default

I want to spare the time typing a -T3 on the command line as I want all the maven builds I will ever do to run multithreaded.
Maven 3.3.9
I googled but didn't find anything promising, only the suggestion to add an environment variable for command line opts and append that to every maven call on commandline.
Maven: configure parallel build in pom.xml
This way, every time, still a manual step is required (entering the environment variable's name) to do a parallel build.
I'd like to configure that globally in the mvn settings.xml file.
You can't configure thing like this settings.xml. This can be configured differently since Maven 3.3.1.
You can use a file ${maven.projectBasedir}/.mvn/maven.config which contains the options given on command like within the root directory of your project which simply can contain:
-T 3
but this is limited on a project base not always.

IntelliJ maven vs normal maven

I would like to add my own commands to delete a specific folder in the mvn repository whenever I run a maven command.
For using maven through the command prompt, this is quite easy since we just update apache-maven-3.5.3\bin\mvn.cmd .
However, I noticed that when we run mvn from intelliJ Maven projects Tool Window, the command run is the following:
C:\mbakOrg\Oracle\JDK\jdk1.8.0_60\bin\java -Dmaven.multiModuleProjectDirectory=
C:\mbakOrg\_CODE\MNE_ARCHIT_GIT\_REPOS\sg-template-store -Dmaven.home=C:\mbakOrg\build\apache-maven-3.5.3 -
Dclassworlds.conf=C:\mbakOrg\build\apache-maven-3.5.3\bin\m2.conf "-javaagent:C:\mbakOrg\devel\JetBrains\IntelliJ IDEA
2017.1.3\lib\idea_rt.jar=42633:C:\mbakOrg\devel\JetBrains\IntelliJ IDEA 2017.1.3\bin" -Dfile.encoding=UTF-8 -classpath
C:\mbakOrg\build\apache-maven-3.5.3\boot\plexus-classworlds-2.5.2.jar org.codehaus.classworlds.Launcher -Didea.version=2017.1.3 clean install
So how will I add a command that will run every time?
Since maven in InteliJis using a custom way to run maven.
Explanation
My problem is basically that the mvn -U command does not properly pull the newest code all the time. Additionally, we are all using a snapshot of a parent project that is being updated quite often to fix issues.
I would strongly discourage modification of mvn.cmd. Even if you figure out how to do it in command line, and in InteliJ, then think about moving to some kind of Continuous Integration framework, like Jenkins for example, which will use default mvn.cmd?
If there is no possibility to achieve what you want with existing Maven tools, I would recommend writing own Maven plugin, (see this tutorial), and put required functionality there. It will guarantee, that this particular piece of code will be executed in all the environments, and this is the way to make sure, that the command will be launched every time.

Maven command line arguments in IntelliJ 14.

I've been learning Maven and absolutely love it, it's an incredible tool. I'm currently running mvn clean, build, compile, package etc from terminal. This works fine but I figure I could do all this from the IDE. However, using -clean as a command line arg in the configuration settings throws an error.
The current command is simply idea:idea. When this runs it appears to be building the project and adding resources but no mention of clean / verify etc.
I've searched various jet brains pages but can't find a commandline list. Ideally I'd like to be able to set up a couple of different configurations, these mimicking the cones that I'd call from the terminal.
thanks for any help
In intellij you have Maven Projects Tool Window which you can use to run any maven build phase or plugin goals, you can also create run configurations from them (just right click and select create).
You can also create maven run configurations by yourself using Run/Debug Configurations dialog. There should be maven type available on the list.
However, using -clean as a command line arg in the configuration settings throws an error.
You should pass just clean as command line argument.
You can read more about how to call maven for example from Maven: The Complete Reference

Which class is Maven calling?

I am looking at an internal Maven application I am building in IntelliJ (same results in Eclipse), and everything looks perfectly clean.
I run clean and compile from IntelliJ (using IntelliJ build or using IntelliJ Maven support) and everything compiles fine.
However when I run the build from the command line (mvn clean compile) I am getting build exceptions (see below).
The command line seems to be looking for a genericized parameter (to the "with" method), which we are indeed passing in correctly. However it is complaining as if we were passing in a non-generic parameter.
Obviously mvn on the command line is using a different version of the "with" method than IntelliJ.
Is there any way to ask Maven to tell me which version of a class it is using, in terms of group, artifact and version of the containing jar?
Here is the precise error
[ERROR] \dev\try-deploy-1\src\main\java\com\victor\webui\WebUserInterface.java:[147,47]
attribute(java.lang.String,org.hamcrest.Matcher<java.lang.String>) in
org.openqa.selenium.lift.Matchers cannot be applied to
(java.lang.String,org.hamcrest.Matcher<java.lang.Object>)
Is it possible that you have another version of the class somewhere? try adding a comment on a new line in the file so the line number changes, and re-run the maven compile to see if it still gives the same line number for the error.
In Eclipse, sorry I know you are using IntelliJ, the maven build usually does an offline build that's why it's so fast. There is a way to force it to do a clean with all the jars though and I use that when I get in a bind. It is the Update Project command and it has a box to force the update of snapshots and releases. Maybe Intellij has something like that? Also I'd verify the version you are using on command line is the same as the one IntelliJ is using (I have a few versions installed). You can check Settings in IntelliJ and
mvn -v
on the command line. Finally I'd make sure the settings.xml files you are using for command line and IntelliJ are the same file.
Good luck.

How do you keep Hudson from giving Maven the -B option for builds?

When Hudson goes to build my project, it executes Maven as follows:
Executing Maven: -B -f /path/to/root/pom.xml clean install
This works fine on most projects. (The -B is for "batch" or "non-interactive mode", BTW).
But for this one project that uses AndroMDA (which I can't recommend for future projects, it's really a pain-in-the-butt; slows down the build by 1000% with code generation for things that could be trivially done with inheritance and annotation-based config).
For some reason unbeknown to me, when Maven is given the -B flag the generated classes are no put on the classpath causing compilation errors for references to the generated classes. I've tested building manually with -B and without it and the result is that it builds fine without -B (outside of Hudson) and it doesn't build with -B (again, outside of Hudson).
Using Hudson version 1.369 and an external Maven 2.2.1 install.
Any advice greatly appreciated!!!
P.S. Hudson is AWESOME!!!!
The simplest version would be to have a free style project, and call maven yourself.

Categories