IntelliJ does not recompile/build maven project - java

I am running into problems when using IntelliJ for Spring development. At first, the IntelliJ build command works fine and picks up changes I make to the source code. At some point, this stops working and I can only compile my sources through maven, as the build or rebuild project commands seem to do nothing anymore.
I am confused, as it works as desired at the start but then stops working at some point.
Steps to recreate:
Create a new Spring project using Spring Initializr:
curl https://start.spring.io/starter.zip -d type=maven-project -d javaVersion=11 -o demo.zip && unzip demo.zip
Start IntelliJ from the command line in that folder, this will auto-import the project: idea64 .
Write a log message in the main class and run the project using Run->Run 'DemoApplication'. This compiles the sources to the target folder and starts up the project. You should see the log message
Alter the log message, run again and the changes should be visible (so far so good)
Now re-compile the sources via maven command mvn compile and restart IntelliJ
From now on, running build or rebuild project within IntelliJ will not pick up any changes I make to any of my source files. In fact both commands don't seem to do anything at all. I have to compile via maven in order to properly compile my sources
I tried a few things to overcome this problem, but without success:
Invalidated caches/restart
Checked project settings for Project Compiler Output and set it to <project-folder>/target
Checked project settings/modules for output path and test output path and put in the path as above
Used Reimport All Maven Projects
Deleted the .idea folder within the project folder. This seems to work, but is very inconvenient and surely no intended behavior. Also it breaks again if I recreate the steps detailed above.
Is anyone experiencing similar problems and/or has a solution?

So for anyone running into the problems I described above:
Apparently deleting the .idea folder and restarting IntelliJ does indeed do the trick. The folder is recreated on restart and afterwards I'm able to switch between compiling within IntelliJ using run, build or rebuild project and compiling through maven goals (mvn compile, mvn test, mvn surefire:test, ...). This persists through restarts of IntelliJ such that I don't have to repeat this everytime I open a project.
I still don't know why this behavior happens, but it's a solution I'm happy to work with as long as I only have to do it every now and then for a project.

Sounds like do don't want Intellij's default build. You want Intellij to forward the build command to mvn. I assume this because you do not mention this step in your question.
Here's a screenshot that shows how to enable it:
More detail is available in the Intellij documentation https://www.jetbrains.com/help/idea/delegate-build-and-run-actions-to-maven.html#delegate_run_action

Related

Issue to start a simple maven project in eclipse?

I have to work with Eclipse due to some reasons which is fresh to me.I created a simple dynamic web project using Maven,then I try to run this project with plugin called tomcat7-maven-plugin,but each time I run it with command tomcat7:run,the console shows build success but the project is automatically terminated.This really confused me,because when I deploy it on my
local tomcat, nothing happened.
from the snippet, I guess the reason is that it tells:
Skipping non-war project
This basically means that your maven project does not produce a WAR (packaging=war) hence tomcat plugin has nothing to run.
I think also that it's not really related to Eclipse here (try to run it from the command line and the results will be the same)
Please restart the eclipse and clean the project. hopefully it will work.

Gradle project doesn't build on my machine, but does on everyone else's

I'm trying to start some work on a Gradle project, but I get a strange error when attempting to build the project on my machine. Currently, we have a Gradle project that builds successfully for all other members of my team, all they need to do is clone the project and it builds without any problems. For myself, that isn't the case.
If I execute the gradle wrapper manually from the terminal:
./gradlew build
the project builds without any errors, but the Gradle synchronisation fails when importing the project into an IDE, giving the following error:
* What went wrong:
Could not resolve all dependencies for configuration ':billing-
provisioning-consumer-finance-details:compile'
I assume that it can't be a problem with the project structure, nor anything wrong with the build.gradle, since it builds without error on other machines.
So far I've tried:
Re-cloning the project (several times)
Clearing the gradle cache at ~/.gradle/caches
Clearing ~/.m2/repositories
Building the project in another IDE (Eclipse), which still gives the same error.
You can follow these steps :
Download Gradle from Gradle
Create or Export GRADLE_HOME as environment variable.Ex: E:\SoftwareRepo\building tools\gradle-4.4.1
Add to Path or Export Path Variable PATH=[YOUR GRADLE_HOME/bin] . ex. E:\SoftwareRepo\building tools\gradle-4.4.1\bin
Open cmd or terminal anywhere in your PC and run gradle --v . If gradle install successfully it will show installed gradle info.
Clone your gradle project
Go to project root directory. Open terminal and run gradle clean build
If there is still error then it will be probably your project is not correctly configured.
If you tried with intelij idea then you need to try this
File->Open. then select your source folder.
then a popup will come select local destribution gradle

Intellij Idea compiler recompiles already compiled project

I use maven command which cleans,builds whole project, creates war and deploys to server. I cannot use Intellij to do that since I have only Community edition. It builds the project in same directory as intellij.
To speed things up I wrote a script which finds compiled files in local "target" directory which are newer than the ones in server and copy them. It all works okay but the problem is Intellij does not see classes compiled with maven as the ones it should skip and rebuilds whole project all over.
Currently it works like this:
Manually in terminal build whole project with maven
Go back to intellij -> make project
Rebuilds all
Run script -> it swaps all files
What I am trying to achieve:
Manually in termin build whole project with maven
Go back to intellij. Change one file -> make project
Compiles only one java file
Run script -> it swaps only one .class file
So the problem is how do I make intellij treat files already compiled with external tool as compiled?
You don't. IntelliJ IDEA has its own incremental compilation system which tracks the dependencies between files being compiled and recompiles the minimum set of classes for every set of changes. External compilation with tools like Maven or Gradle does not update IntelliJ IDEA's incremental compilation database. Because of that, IntelliJ IDEA cannot recognize the fact that classes have been already compiled with an external tool, and will recompile.
That troubled me for long time. Finally, i found this .
IDEA build settings
You can choose whether use InteliJ or gradle to compile when runnning program. Under gradle project, it uses gradle to build by default.

Java project runs in IDE (IntelliJ Idea) but fails to run after Maven build

I have a Java project that is ready to ship, but i have stumbled at the last hurdle. The program runs fine in the IDE, but after i run mvn clean install and the target jar is created successfully, the application fails both on the command line AND in the IDE. I can get it to run again in the IDE by doing a rebuild from within the IDE.
Has anyone else experienced something similar?
The error after the mvn clean install is
2014-08-08_10:38:20.328 INFO coza.modh.fxplatform.Controller - Application start
2014-08-08_10:38:20.344 INFO c.m.f.c.p.DataSourceFactory - Connecting to Database.
2014-08-08_10:38:20.499 INFO coza.modh.fxplatform.Controller - Model created
Exception in thread "main" java.lang.NullPointerException
at coza.modh.fxplatform.view.MainView.<init>(MainView.java:46)
at coza.modh.fxplatform.Controller.startup(Controller.java:40)
at coza.modh.fxplatform.Application.main(Application.java:7)
The line it fails on looks like this:
setContentPane(contentPane);
If i rebuild from within the IDE then the variable is not null when it gets here, but after the mvn command the variable is null when it gets to this point.
EDIT: Someone pointed out that it might be related to the fact that i used the intelliJ GUI designer tools and that Maven might not gel with it. Possibility?
I had a similar problem today. To solve the issue -
Firstly, add the following to the pom.xml
<dependency>
<groupId>com.intellij</groupId>
<artifactId>forms_rt</artifactId>
<version>5.0</version>
</dependency>
Secondly, as you stated, use the correct maven plugin to create the build.
See this for step by step instruction.
Unlikely that contentPane is null, but check. Do a clean build anyway. Maybe the line number is not entirely correct.
Normally the problem are resources, like an application icon.
They are not File but zipped inside the jar.
So paths like /src/main/resources/... will not do.
Neiter using a backslash \, and
furthermore the file paths are case sensitive.
This can be caused by different library versions, do you have changed the build path in Eclipse? (Project->Properties->Build Path -> Libraries). There should only be "JRE System Library". If there is more, then it is possible that it is be loaded instead of one of your Maven dependencies. You using "mvn clean install" means it is compiled against the Maven dependencies and thus it runs differently.
Install Maven on the system(not having IDE) before running the code
ensure you've got system property M2_HOME set to your Maven installation location and also Maven bin folder added to PATH
go to the directory,containing pom.xml of your project and type "mvn install" plus Enter to build your project
if it's for example simple project,packaged as JAR,you can run it then by typing something like this in the console:
$ java -cp target/simple-1.0-SNAPSHOT.jar org.yourcompany.projectNam.App
and Enter

Class Not Found Exception when running JUnit test

I am getting this error when running JUnit test in Eclipse:
Class not found com.myproject.server.MyTest
java.lang.ClassNotFoundException: com.myproject.server.MyTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:693)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:429)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
I've tried adding JUnit library in the classpath although I'm using maven, and the JUnit library is in the POM dependency.
I already have tried cleaning the project and created a new JUnit test case using the JUnit plugin for Eclipse, still getting the same error.
This appears to occur because only the source code is compiling when you use mvn clean compile (I'm using maven 3.1.0 so I'm not sure if it always behaved like this).
If you run mvn test, the test code will compile as well, but then it runs the tests (which may not be immediately desirable if you're trying to run them through Eclipse.) The way around this is to add test-compile to your Maven command sequence whenever you do a mvn clean. For example, you would run mvn clean compile test-compile.
I had a similar problem with my tests and found somewhere in the Web, that you have to go to Build Path in your project's properties and move Maven Dependencies above JRE System Library. That seems to have worked for me.
I faced the same problem and I was able to fix it using #slomek's answer but the issue resurfaced after that.
I finally fixed it by adding my project's output folder to the JUnit test's run configuration. Steps are:
Find your project's output folder from Project properties -> Java Build Path -> Default output folder
Usually this is <project-folder>/bin
Go to Run -> Run Configurations...
Click on the unit test's run configuration and click on the Classpath tab
Explicitly add your project's output folder under User Entries - even if the project is already included there
Click on Advanced -> Add folder to add the output folder
This issue might be due to weird project setup in Eclipse - e.g. source folder with nested maven projects imported as a single project/folder (at least that was how my project was setup).
If this problem occurs in Eclipse only, executing command Project -> Clean... on selected project may help.
After having tried everything here with no improvement, I solved my issue by just restarting Eclipse
I fixed my issue by running maven update.
Right click project your project > Maven > Update Project
In my case I had a wrong maven directory structure.
Which should be like:
/src/test/java/ com.myproject.server.MyTest
After I fixed that - everything worked like a charm.
In my case, only next steps helped me to resolve this issue:
Project->properties->Run/Debug Settings.
In "Launch configurations for '.....'" select classes/projects
Edit -> Classpath -> "Restore Default Entries"
I had the similar problem with my Eclipse Helios which debugging Junits. My problem was little different as i was able to run Junits successfully but when i was getting ClassNotFoundException while debugging the same JUNITs.
I have tried all sort of different solutions available in Stackoverflow.com and forums elsewhere, but nothing seem to work. After banging my head with these issue for close to two days, finally i figured out the solution to it.
If none of the solutions seem to work, just delete the .metadata folder created in your workspace. This would create an additional overhead of importing the projects and all sorts of configuration you have done, but these will surely solve these issue.
Hope these helps.
Earlier, in this case, I always did mvn eclipse:eclipse and restarted my Eclipse and it worked. After migrating to GIT, it stopped working for me which is somewhat weird.
Basic problem here is Mr Eclipse does not find the compiled class.
Then, I set the output folder as Project/target/test-classes which is by default generated by mvn clean install without skipping the test and proceeded with following workaround:
Option 1: Set classpath for each test case
Eclipse ->Run ->Run Configurations ->under JUnit->select mytest -> under classpath tab->Select User Entries->Advanced->Add Folder -> Select ->Apply->Run
Option 2: Create classpath variable and include it in classpath for all the test cases
Eclipse ->Windows ->Classpath Variables ->New->[Name : Junit_test_cases_cp | path : ]->ok
Then go to
Eclipse->Run ->Run Configurations ->JUnit->select mytest ->under classpath tab ->Select User Entries->Advanced->Add classpath variables->Select Junit_test_cases_cp->ok->Apply->Run
This is the only thing currently working for me after trying all the suggestions online.
NoClassDefFoundError really means it can't initilize the class. It has nothing to do with finding the class. I got this error when calling trim() on a null String.
JUnit won't show NullPointerException. The string isn't null when running normally because
I'm fetching the string from a properties file which is not availible for tests.
My advice is to remove pieces from the class until your tests start passing. Then you can determine which line is giving the error.
In my case, changing the order of Maven Dependencies from Build Path configuration did not work for me. I changed its order from Run configuration.
Follow these steps:
• Go to Run -> Run Configurations...
• Click on the unit test's run configuration and click on the Classpath tab.
• check Use temporary JAR to specify classpath (to avoid classpath length limitations).
May be it is enough and your test works. You should try, so press Run button. If it did not work. Follow all previous steps again and without pressing Run button, go to the next step.
• In sub branches of User Entries move Maven Dependencies above your project or test class.
• Click Run button.
These steps worked for me.
Delete the content of local Maven repository.
run mvn clean install in the command line. (cd to the pom directory).
Build Project in Eclipse.
1- mvn eclipse:eclipse
2- project clean all projects
3- restart
It's worth mentioning as another answer that if you're using eGit, and your classpath gets updated because of say, a test coverage tool like Clover, that sometimes there's a cleanup hiccup that does not completely delete the contents of /path/to/git/repository/<project name>/bin/
Essentially, I used Eclipse's Error Log View, identified what was causing issues during this cleanup effort, navigated to the source directory, and manually deleted the <project name>/bin directory. Once that finished I went back to Eclipse and refreshed (F5) my project and the error went away.
It can also be due to "[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?"
I had the same problem with a Gradle project with a test SourceSet with two resource directories.
This snippet comes from a main-module.gradle and adds a resource dir to the test SourceSet:
sourceSets {
test {
resources {
srcDir('../other-module/src/test/resources')
}
}
}
Doing this I had two resource directories related to the test SourceSet of the project main-module:
../other-module/src/test/resources
src/test/resources (relative to the main-module folder, automatically added by the java plugin)
I find out that if I had two files with the same name in both the source directories, something in the process resources stage went wrong. As result, no compilation started and for this reason no .class were copied in the bin directory, where JUnit was looking for the classes. The ClassNotFoundException disappeared just renaming one of the two files.
It seems compile issue. Run project as Maven test, then Run as JUnit Test.
For project that does not use maven : This worked for me https://ihategeek.wordpress.com/2012/04/18/eclipse-junit-test-class-not-found/
Adding the jre and project src at the bottom in Order and exports in build path
Pls check if you have added junit4 as dependency.
e.g
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
For me I had to put the project x/src/test/java/ at the bottom of the "order and export" in the "java build path"
Check if your project is opened as a Maven project and not just a regular Java project. Actually a no-brainer, but that is exactly the same reason why you might miss it.
I had faced the same issue. I solved it by removing the external JUnit jar dependency which I added by download from the internet externally. But then I went to project->properties->build path->add library->junit->choosed the version(ex junit4)->apply.
It automatically added the dependency. it solved my issue.
Making some dummy change and saving the test class can solve the problem. It will build the .class automatically
Might be you forgotten to place the Main class and Test Case class in
/src/test/java. Check it Once.

Categories

Resources