Intellij IDEA rebuilds entire module each time jUnit test is run - java

I have a large project with several modules including one large "src" module, and each compilation takes at least 5-10 minutes. I'm unable to refactor the structure of the project to potentially speed up compilation.
Every time I try to run a JUnit test, IntelliJ always compiles the entire module before running the tests (even if no files changed).
Other answers suggested using the Eclipse compiler and the "Make, no error check" launch command instead of the regular "Make". I tried that but IntelliJ is still rebuilding the entire module.
Edit: This seems to be related to how errors and handled with "Make, No Error Check". My project contains errors in unrelated areas of the code (managed by other teams) that I was using the eclipse compiler to skip over. After "fixing" those errors, incremental compilation works again. Maybe the build is considered invalid (and is discarded) even if errors are skipped?

Change your run configuration to not make the module:
Go to Run -> Edit Configurations (or click the Edit Configurations from the Run dropdown menu), and you'll see this screen:
Remove "Make" from Before launch, and it should work
P.S. I would suggest renaming it to something like: JUnit tests (NO REBUILD) otherwise you might be in for some serious head-scratching later on :)

Related

First Run of Maven Built Application in IntelliJ fails

I have a project that I want to build with Maven but run with IntelliJ, i.e. not delegate run actions to Maven. I have an Application Run Configuration that points at the main class of my project, with a Before Launch action of 1. Run Maven Goal 'Project Name: compiler:compile'. The FIRST time only that I run this config, I get:
Error: Could not find or load main class xx.Main
Caused by: java.lang.ClassNotFoundException: xx.Main
And on subsequent (using the exact same run config) runs, it works perfectly.
Note: if I have a separate Maven Build Target for my project and I run that first and then the Application Run Target (without the build before launch), it works first try. I've also tried setting a Before Launch action to run that Build Target, with the same failure as a result. This ONLY occurs when I try to build and run in the same target.
It seems like IntelliJ is caching the build state BEFORE anything runs and then using that same state AFTER the Maven Build has run... Does anyone know why this occurs and if there is a way to fix it, or am I just stuck running it twice after every clean? Happy to provide more detail if required.
Background as to why I want to do it one step: I'm setting this up for some very junior (and VERY prone to messing things up) devs and the fewer steps there are, the better. I don't want them to have to think about how to make it run.
As suggested by tgdavies in a comment, it's possible to make this work first time by adding an IntelliJ Build task following the Maven Build task in the Before launch steps. My Main Run Target now has Before launch: 1. Run Maven Goal 'Project Name: compiler:compile' 2. Build. Obviously this solution is less than ideal but at this stage, I'll take functional. I'll be raising a ticket with IntelliJ and hopefully it will be addressed... Thanks tgdavies!

How to update external jar file without breaking everything with gradle?

When updating an external jar file which contains breaking changes, so the code at certain points will have errors (which happens and is fine), I am unable to build my project via gradle, thus EVERY import from the jar shows up in my IDE as an error (Intellij).
This makes it super hard to actually see where the errors are, since ALL imports from the jar, even though all those classes are in there show up as "errors", since build failure means gradle simply wont import the jar at all...
The end result is that my IDE shows things like this (removed package name for security reasons):
All of those model.entity classes ARE in the jar, but gradle wont import them because the build process stops at the first syntax error in the code...
Is there any way around this? How to refactor large code bases without relying on the error messages inside the gradle console (which there might be a LOT of).
I'd like to import the jar first THEN fix the syntax errors? Maybe a different gradle command? I'm simply using "clean build"
There are 2 aspects here.
grade
IDE (appears to be Intellij) - imports the gradle to create project. unless on auto refresh, requires manual reimport (refresh button) when there are changes in gradle files
in your scenario, initial state is gradle build works, Intellij works fine
you replace the dependency with 'breaking' change.
gradle build - will fail at compile stages as code is yet to the fixed
IntelliJ - still is not seeing new dependency. click the re-import
The import will always succeed unless there are errors in .gradle files
After import IDE will now reflect new dependency

IntelliJ IDE compiles another first and if it does not have any error it compiles the one I clicked for

I have a project named "Another one". It has 2 programs which do not have any relation with each other. The programs are "abc.java" and "def.java".
Both programs are for printing "Hello World".
If "abc.java" does not have any mistakes then only the compiler compiles "def.java",even though "def.java" does not have any mistakes.
Do I have to create one project for each program I make. I am new to programming.
def.java works fine here
when I removed a semicolon from abc.java, def.java stops working
Project structure
Module Structure
First, in IntelliJ you don't think in terms of "programs", but in terms of "project" and "modules". The most basic one is a project where the single module is located where the project is located (e.g. Project at D:\projects\MyProjects with 1 module at D:\projects\MyProjects).
What I can think of going wrong:
Your module containing abc.java is not actually a module. if you can execute abc.java with a run configuration, it should be a module.
Your build settings are somewhat wrong.
Compiler errors should keep you from executing the program to prevent misbehaviour in your application. For me, I can only not execute a module if the other modules have any compiler error while also being a dependency for the executing module. But not sure if a tool/plugin is causing this behaviour.
Also, you can check if you set up your modules correctly. Check the image above if your configuration looks similiar.
I hope I can help you. If not, maybe update your post providing some screenshots of your setup and configurations as I have shown you.
Get your definitions and terms right before doing anything else.
And then tell people what you're actually doing.
As stated, IntelliJ (and indeed all modern IDEs) have modules and projects. When you execute a target in a module, that entire module gets built IF NEEDED.
If "abc.java" does not have any mistakes then only the compiler compiles "def.java",even though "def.java" does not have any mistakes
This does not make any sense whatsoever. The compiler will compile whatever needs compiling. But most likely you're not talking about compiling, you're clicking the "run" button which will run whatever it is configured to run.
If you only have 1 run configuration set up, that's what will ALWAYS be run pressing that button.
If you want to run something without creating an explicit run configuration, just right click the open source file and select "Run".
This will cause ALL changed sources in the module to be compiled, no mumbo jumbo about it "compiling x despite y not having errors".
It will also attempt to compile everything that does have errors, and fail.

Continuous testing with IntelliJ Idea

I'm looking for an IntelliJ IDEA plugin that would run my tests each time I change my code. I looked for such a solution and I found:
Infinitest, which works, but is inconvenient because I need to add the facet to each module, and it opens a new tool window for each module (which means 15 tool windows for me).
Fireworks - didn't work for me, maybe it just doesn't work with IDEA 14 (in its repo I can see that last changes were made in 2009). IntelliJ also reports that it throws exceptions.
There are lots of ways I could run all my tests (including writing a simple script for this), but I'm looking for a tool that would be smart enough to rerun failed tests first, and that would understand module dependencies (so after a change in some module it would run only tests of dependent modules).
I prefer free options, but if there's something paid for a reasonable price, I would accept it as well.
IntelliJ now actually has a Toggle auto-testin the run dialog. Just run a run-configuration and then select Toggle auto-testin the run dialog.
It's not as intelligent as you would have hoped. It just reruns when it detects changes.
I know this is a 3-year-old question but I think it will help people who face the similar problem in future. So I found out a way to enable SBT style auto test execution in IntelliJ studio.
We need to do 2 things to enable auto test execution.
Enabling auto project build - This can be done in settings by going into File -> Settings -> Build, Execution... -> Compiler and selecting "Build Project automatically".
Enabling "Toggle auto-test" in run dialog box
This will start auto testing. Although this works fine, it takes time to build the project even when my project is tiny so for larger projects it will certainly take very long time to complete the build and execute tests.
Reference: Original article which explains these steps
If you are OK running tests which cover a single method chosen by you, you can try this plugin (it is continuos in the sense that you make changes to a method, then click on the method and the plugin will run all the unit tests automatically which cover that method): https://plugins.jetbrains.com/plugin/15063-unit-test-coverage-history-runner
You can use the Intellij Teamcity plugin. Teamcity is a paid product but there is a free version which gives you 20 projects and 3 agents for free .
It has a remote run feature using which you can send in unchecked code to run tests before committing.
It also has options to run failed tests first
Usage instructions for Remote Run

IntelliJ. Debug a program even if it does not compile

I have a Java project in IntelliJ that compiles, and now I am slowly changing.
Is there a way to ask IntelliJ to run the project, even if some parts of the code still do not compile? If so, how?
Why I would need this, you ask? see this ticket:
IDEA-61945 Run and Debug commands should ignore compile errors not related to the main being run.
http://youtrack.jetbrains.net/issue/IDEA-61945?query=it#tab=Comments
Please, do not answer this post questioning whether I should or I should not need to run a project even if it does not compile. please.
If not possible in IntelliJ, is it possible in maven? How?
In Intellij 12 you also have the following option (which personally I find the best one):
Go to Edit configuration of your launcher
Go to the before launch section
If 'Make' is in the list: remove it
Add 'Make, no error check'
Now, when you run, a make will still be done automatically but the run will continue even if the are compilation errors.
Regarding the above debate; I think it makes perfect sense to be able to run a part of the code that does compile even if another part of the code does not not; e.g. if that other part of the code belongs to a module that is in your project but not involved when running.
I ran into this exact same problem at work today. Before now, I probably would have been quick to jump on the bandwagon of, "Why would you ever want to do that?" Turns out that Eclipse lets you do exactly this, and if you start working concurrently with other developers who depend on this feature (which is to say, check in code that doesn't compile), it's handy to be able to do the same in IDEA!
And lucky for us IDEA users, you can. Follow these instructions from the FAQ for Eclipse Users, and you're good to go:
To be able to run code with errors, you can select the Eclipse
compiler in Settings dialog, Compiler, Java Compiler and add the
-proceedOnError option to the Additional command line parameters for the compiler.
The only thing that's lame is that it's not quite as seamless as in Eclipse. First, you'll have to untick the option to Make before run because IDEA won't run if make fails. Then, you'll have to remember to build before running. With those caveats, though, you should be able to accomplish what you're after.
Stijn Geukens's answer is correct, but it can be improved.
In Intellij Idea version 12 instead of removing the "Make" rule it can be replaced with "Make, no error check". This way project will be rebuilt (compiler will atempt to do it), but it will run the program independently of compile outcome.
If you want to debug just one part, then you can create a unit test around that. If you do not use the class that does not compile, then you can still debug the unit test related code.
When there are compilation errors, you can exclude specific files from compilation.
Go to the Messages window (if it is not visible: View -> Tool Windows
-> Messages)
Right click the problem file
Exclude from compile
At least in Intellij 12 you can achieve this.
First try to compile the project, including the broken class(es).
Then in the Messages view, containing all the compile errors:
Right-click the class you want to exclude
Click 'exclude from compile'
See this question on how to reinclude afterwards.
For Intellij 2017.3.1 my configuration is like this:
Use the Eclipse Compiler: Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler -> Use compiler: Eclipse
Select "Proceed on errors"
Edit your desired configuration defaults (I use this for JUnit) before launch to Build, no error check: Check this screenshot
Additional step in order for Intellij not to open the classes with errors when you run your configuration. Un-select Automatically show first error in editor in Settings -> Build, Execution, Deployment -> Compiler
PS: This configuration is not perfect for all usages. It only works when you are fixing unit tests that were failing because of your changes in implementation code. When you go back to implementing features it is more useful to disable this feature since it will let you run you implementation code with errors and it will not jump to compilation errors. You need to go back and forth with changing the Eclipse compiler with the Javac one for best results.
Wow, it's been a while since I've been in IntelliJ and I miss it dearly! From my recollection you should be able to right click the main method in a module and run it directly so long as the remainder of the files in the module compile. I don't think it matters that a second module in the same project has errors. Is that not working for you?
updating for version 2017 - 2.5 community as menu options are slightly different
Navigate to:
Run>Edit Configurations
near the bottom of the Run/debug config window look for
"before launch: Activate tool window "
the field below this heading lists your current build config settings.
Use the + and - symbols in order to add and remove build preferences.
Once completed
Select apply then Okay
Thats it!
I don't think its possible at all. How you can run something that doesn't compile? That would be like driving a car that isn't put together. You could comment out the files that don't compile, so that the project compiles....
Edit -- or you can have Intellij not count the file as source by
Right Click on your project -> open module settings -> select your module -> select the file -> excluded

Categories

Resources