Which class is Maven calling? - java

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.

Related

error in my code that code is not running [duplicate]

What are the possible causes of a "java.lang.Error: Unresolved compilation problem"?
Additional information:
I have seen this after copying a set of updated JAR files from a build on top of the existing JARs and restarting the application. The JARs are built using a Maven build process.
I would expect to see LinkageErrors or ClassNotFound errors if interfaces changed. The above error hints at some lower level problem.
A clean rebuild and redeployment fixed the problem. Could this error indicate a corrupted JAR?
(rewritten 2015-07-28)
Summary: Eclipse had compiled some or all of the classes, and its compiler is more tolerant of errors.
Long explanation:
The default behavior of Eclipse when compiling code with errors in it, is to generate byte code throwing the exception you see, allowing the program to be run. This is possible as Eclipse uses its own built-in compiler, instead of javac from the JDK which Apache Maven uses, and which fails the compilation completely for errors. If you use Eclipse on a Maven project which you are also working with using the command line mvn command, this may happen.
The cure is to fix the errors and recompile, before running again.
The setting is marked with a red box in this screendump:
try to clean the eclipse project
you just try to clean maven by command
mvn clean
and after that following command
mvn eclipse:clean eclipse:eclipse
and rebuild your project....
Your compiled classes may need to be recompiled from the source with the new jars.
Try running "mvn clean" and then rebuild
The major part is correctly answered by Thorbjørn Ravn Andersen.
This answer tries to shed light on the remaining question: how could the class file with errors end up in the jar?
Each build (Maven & javac or Eclipse) signals in its specific way when it hits a compile error, and will refuse to create a Jar file from it (or at least prominently alert you). The most likely cause for silently getting class files with errors into a jar is by concurrent operation of Maven and Eclipse.
If you have Eclipse open while running a mvn build, you should disable Project > Build Automatically until mvn completes.
EDIT:
Let's try to split the riddle into three parts:
(1) What is the meaning of "java.lang.Error: Unresolved compilation
problem"
This has been explained by Thorbjørn Ravn Andersen. There is no doubt that Eclipse found an error at compile time.
(2) How can an eclipse-compiled class file end up in jar file created
by maven (assuming maven is not configured to used ecj for
compilation)?
This could happen either by invoking Maven with no or incomplete cleaning. Or, an automatic Eclipse build could react to changes in the filesystem (done by Maven) and re-compile a class, before Maven proceeds to collect class files into the jar (this is what I meant by "concurrent operation" in my original answer).
(3) How come there is a compile error, but mvn clean succeeds?
Again several possibilities: (a) compilers don't agree whether or not the source code is legal, or (b) Eclipse compiles with broken settings like incomplete classpath, wrong Java compliance etc. Either way a sequence of refresh and clean build in Eclipse should surface the problem.
I had this error when I used a launch configuration that had an invalid classpath. In my case, I had a project that initially used Maven and thus a launch configuration had a Maven classpath element in it. I had later changed the project to use Gradle and removed the Maven classpath from the project's classpath, but the launch configuration still used it. I got this error trying to run it. Cleaning and rebuilding the project did not resolve this error. Instead, edit the launch configuration, remove the project classpath element, then add the project back to the User Entries in the classpath.
I got this error multiple times and struggled to work out. Finally, I removed the run configuration and re-added the default entries. It worked beautifully.
Just try to include package name in eclipse in case if you forgot it
Import all packages before using it, EX: import java.util.Scanner before using Scanner class.
These improvements might work and it will not give Java: Unresolved compilation problem anymore.
Also make sure to check compiler compliance level and selected jdk version is same
As a weird case, I encountered such an exception where the exception message (unresolved compilation bla bla) was hardcoded inside of generated class' itself. Decompiling the class revealed this.
I had the same issue using the visual studio Code. The root cause was backup java file was left in the same directory.
Removed the backup java file
When the build failed, selected the Fix it, it cleaned up the cache and restarted the workSpace.

Maven dependency:list shows different version than is being executed

Debugging my program in IntelliJ, I can see that the dependency called registrationcore-api is being run from the latest code in GitHub. However, running mvn dependency:list from my project directory shows
[INFO] org.openmrs.module:registrationcore-api:jar:1.8.0:provided
i.e., the last release version, from last October. IntelliJ displays a warning when the debugger hits this module, saying "Source code does not match bytecode." In the editor it displays the source code for release 1.8.0.
I did recently clone registrationcore-api and do some work on it. But then I deleted the source code, and deleted the directories from the maven repository (rm -r ~/.m2/repository/registrationcore*), so that it would be downloaded on next run. Somehow, somewhere, the bytecode for the version I was working on has stuck around.
Where is it? How can I get rid of it? How is it linked in to my application, and why doesn't Maven know about it?
EDIT: I've run mvn clean and ensured that IntelliJ and the installed Maven are using the same repository location (~/.m2/repository).

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.

How to get current Git branch name in Eclipse run configurations

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

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

Categories