First Run of Maven Built Application in IntelliJ fails - java

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!

Related

What tasks do I need in order to be able to run my Gradle-based Java project from IntelliJ IDEA?

I have a large mostly-Java multi-project Gradle build that I have opened in IntelliJ IDEA. This was imported correctly (as far as I can tell). One of the projects inside this is an old-school Java Swing GUI application.
When I attempt to run its main class from within IntelliJ using the "Run Main.main()" context menu, IntelliJ launches a Gradle task ":folder:project:Main.main()", which fails because of course there is no task in this project's build.gradle called "Main.main()".
The exact error looks like:
Execution failed for task ':folder:project:Main.main()'.
> A problem occurred starting process 'command 'C:/path/to/ojdk8/bin/java.exe''
What exactly am I expected to put into my build.gradle to get this to work?
As a secondary question, is it possible to make such a task agnostic over the actual main()-method containing class? This project contains several such classes that expose different functionality.
For context, I am a long-time Eclipse user evaluating IntelliJ for my organization. This is on IntelliJ IDEA 2022.1, Gradle 5.6.4, OpenJDK 1.8.0_275
I feel like I am missing something completely obvious, but I could not find the answer with any web searching or reading the IntelliJ documentation.
My mental model of what was happening was wrong. No specific Gradle tasks are required.
It turns out the root cause was Windows. When re-running the task with --stacktrace option I was able to see the actual error, which turned out to be:
CreateProcess error=206: The filename or extension is too long
Googling with this info I found this previous Stackoverflow question. Its answered got me onto the "Shorten command line" flag in the Run/Debug configuration. Setting this to "JAR manifest" solved the problem.

Jenkinsfile path location "#2 converted to %402" and build is failing

I have one Jenkins job which does following things
Checks out the Jenkinsfile from github at some location (c:\jenkins\workspace\my_build)
Jenkinsfile checks out java source code to (c:\jenkins\workspace\my_build#2)
mvn clean install
When I run "mvn clean install" on my build machine it works perfectly fine.
But when I run it thorough Jenkisnfile I have few unit test cases which runs when building my project, those units tests are failing with java.io.FileNotFoundException(The system cannot find the path specified) exception/error.
When I ran maven in debug mode(using -X) I found out the workspace path(c:\jenkins\workspace\my_build#2) is being converted to c:\jenkins\workspace\my_build%402 hence maven is unable to find the file which is required for my unit test cases to pass.
How can I fix this issue?
I managed to fix the issue by using a custom workspace. Something like this
ws("c:\jenkins\my_custom_location") {
// git checkout
// mvn clean install
}
Jenkins didn't create any directory with #2 or #3 when using custom workspace.
#user3847894,
You did not fix the issue, merely worked around the problem (avoidance). Now, if you run builds in parallel, they will all use the same workspace, probably with horribly unintended consequences.
You can try choose a different symbol:
hudson.slaves.WorkspaceList
Since: 1.424 Default:
# Description: When concurrent builds is
enabled, a unique workspace directory name is required for each
concurrent build. To create this name, this token is placed between
project name and a unique ID, e.g. "my-project#123".
Or figure out the real problem:
where your system is pulling the wrong character set (ANSI vs UTF-8 ? locale), encoding or something is wrongly "sanitizing the path" (eg: OWASP Sanitizer).
You'd have to provide way more info re: OS, jdk, system and startup parameters, etc to diagnose, list of plugins (maven and Jenkins), so can't help further. Check controller and agent system info (${JENKINS_URL}/systemInfo and ${JENKINS_URL}/computer/myNode/systemInfo) and also see what maven reports in the settings and help:system. On your own; good luck

Intellij IDEA rebuilds entire module each time jUnit test is run

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 :)

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

Using multiple projects in Eclipse with Play framework 2.1.x

I have an existing project in Eclipse (let's call it "NotPlayProj") which has a lot of java code still under development. I made a new eclipse project using play 2.1.0 (let's call it "PlayProj"). My goal is to use code from NotPlayProj in PlayProj and have both Eclipse and the Play compiler notice changes in either project.
If I go into the properties for PlayProj and add NotPlayProj via the Project tab, then method completion and inclusion works within eclipse, but the Play compile doesn't see the result. I've looked at Play modules and those don't seem to do what I want.
Is there any way to do this, ideally without modifying the NotPlayProj?
Edit ---
I've looked at http://www.playframework.com/documentation/2.0/SBTDependencies which shows how to export a jar from NotPlayProj into the PlayProj/lib directory, but this requires a manual export for each time NotPlayProject changes. I suspect that the Managed dependency section is supposed to cover this, but I've never used SBT before and am therefore probably missing something basic.
What you need is continuous integration.
Have a look at Jenkins: http://jenkins-ci.org/
You should setup a Continuous integration server and customize the builds you need.
Example:
You have your PlayProj running in some server, it needs to be able to use some of the latest classes from the other project called NoPlayProj.
Rebuild is a must, things such as downtime zero are difficult to achieve(At least I don't think this is what you are asking for either).
The steps you need to automate with Jenkins are:
1 - Build and deploy the latest version of NoPlayProj which is located in some repositorium
2 - Build and deploy the latest version of PlayProj which is located in some repositorium and also is contains your last commit where you updated the dependency that exist with NoPlayProj
A not very complex build and deployment instructions can be configured in Jenkins. This should speed you up a bit.
Also another suggestion would be to mavenize both projects if possible, this will help you manage the dependencies easier.
Just to clarify one thing, you said: My goal is to use code from NotPlayProj in PlayProj and have both Eclipse and the Play compiler notice changes in either project.
Well the order in which you execute the builds will be dependent in what you want to do as long as you update the dependency before you commit the code.
One last thing, if you don't want to deploy you don't have to do so you can create the Jenkins jobs, in such ways that you only build. With Jenkins you can do a lot of stuff, also you could execute some help scripts of your own that can provide you additional functionality.
I hope this was useful.
To let Eclipse see changes in NotPlayProj when working with PlayProj, it's enough to change configuration of PlayProj. Properties-> Java build path -> Projects -> Add NotPlayProj as dependency.
There is no straightforward way to let Play compiler handle dependencies, until you package it as jar. Consider configuration of simple ant task (External tools configuration -> Ant build ), which will copy your jar file. Task can be triggered by pressing the key or button.
With managed dependencies, every time you made change in NotPlayProj, you have to manually rebuild it. To let Ivy/Maven put dependency in your local repository. After that Play will take latest snapshot from your local repository.
Both approaches requires some efforts. Maybe you can take a look at Python scripts, which run Play, maybe it's enough to extend classpath with NotPlayProj when executing play start
Though I've never used the play framework, I would think that there is a format that both the play framework and eclipse understand and that is Maven. Look at http://www.playframework.com/modules/maven-head/home

Categories

Resources