IntelliJ Tomcat Automatic Redeploy when running a gradle task - intellij-idea

What I am trying to accomplish:
I want the tomcat 9 server to execute an automatic redeploy when I run a Gradle task that updates my HTML files.
This is my setup:
I use IntelliJ 2020.03 (ultimate edition), tomcat 9, the application is a GWT application.
My Configuration for Tomcat:
This is what I see when I hit the "Configure ..." button next to the line labeled "Application server:"
This is my Gradle task I run but tomcat does not automatically redeploy the changes
Things I already tried:
According to [https://www.jetbrains.com/help/idea/updating-applications-on-application-servers.html] I should have an option to "Update resources". But my options are limited to:
Therefore I assume I need to have "Exploded artifacts in local application server run configurations".
Therefore I headed to Tomcat -> Edit Configuration
I replaced the deploy to the exploded artifact:
Using this I get the following error message on starting up tomcat:
[2021-02-12 08:46:05,533] Artifact Gradle : NewComApp.war (exploded): com.intellij.javaee.oss.admin.jmx.JmxAdminException: com.intellij.execution.ExecutionException: C:\Users\heckner\IdeaProjects\NewComApp\build\libs\exploded\NewComApp.war not found for the web module.
So I decided to compare the artifact that "works" (but does not update the HTML files) with the "exploded" artifact which would be probably the right one but throws an error message on startup of tomcat.
This is the one which works ("NewComWar.war"):
This is the one which does throw an error message on startup ("NewComApp.war (exploded)":
As you can see in the image under "... which works". the war already seems to be "exploded". So why does IntelliJ does not offer the "update resources"?
But never the less, when I switch in Tomcat Edition to "NewCompApp.war (exploded)" i am able to select "update resources" in the drop down:
So probably this would be the way to go.
It obviously boils down to the point: What is wrong with the artifact declaration above so that tomcat throws the error message?
The feedback was: "ctually "NewComWar.war" is an archive that contains exploded artifact, that's why only "Redeploy" is possible. Please check that exploded artifact is created in "Output directory". "
Now the question is how I can add the exploded war to the Output Directory?
I tried:
but then I can only select from:
When I add this, it looks like this:
When I run Tomcat, it still says:
[2021-02-12 12:24:54,224] Artifact Gradle : NewComApp.war (exploded): com.intellij.javaee.oss.admin.jmx.JmxAdminException: com.intellij.execution.ExecutionException: C:\Users\heckner\IdeaProjects\NewComApp\build\libs\exploded\NewComApp.war not found for the web module.
Now I found the following tip (thanks Evgeny):
https://youtrack.jetbrains.com/issue/IDEA-178450#focus=streamItem-27-4068591.0-0
I switched under Settings -> Build, Execution, Deployment -> Build Tools -> Gradle: "Build and Run:" IntelliJ IDEA
I added this snipped to build.gradle:
task explodedWar(type: Copy) {
into "$buildDir/libs/exploded/${war.archiveFileName.get()}"
with war
}
war.dependsOn explodedWar
I switched the artifact which is deployed to the tomcat to
this automatically added the Gradle task:
Build 'Gradle:NewComApp.war (exploded) artifact to the
which is defined like this:
This accomplishes two things:
I can choose "Update resources" on my Edit Configuration for Tomcat like shown below:
My deployment runs well under tomcat
But ... :-)
Updates to the HTML files (within the war file) are not exploded to the NewComWar.war directory.
When I start tomcat I see the following file structure under C:\users<myname>\IdeaProjects\NewComApp\Libs\
The reason for this is that we use a Gradle task that generates the HTML files.
This task is called "copyHTML"
Under build.gradle it is defined now as follows:
war {
from 'war'
dependsOn copyHtml
exclude excludeInWar
doFirst {
manifest {
def version = ant.hasProperty('gitversion') ? ant.gitversion : 'undefined version'
println "Version: ${version}"
attributes("Implementation-Title": project.name, "Implementation-Version": version, "Built-By": new Date())
}
}
}
task explodedWar(type: Copy) {
into "$buildDir/libs/exploded/${war.archiveFileName.get()}"
with war
}
war.dependsOn explodedWar
copyHtml {
dependsOn generatorClasses
inputs.dir 'html'
inputs.dir 'email'
inputs.dir 'email.Tags'
inputs.dir props.getProperty('generator.htmlfiles.prefix') + 'html'
inputs.dir props.getProperty('generator.htmlfiles.prefix') + 'html.MeetingApp'
inputs.dir props.getProperty('generator.htmlfiles.prefix') + 'staticHtml'
inputs.properties props
outputs.dirs 'war', 'resources/com/newcomapp/server/mail'
doFirst {
ant.properties["generator.classpath"] = sourceSets.generator.runtimeClasspath.getAsPath()
}
}
task warWithoutGwt(type: War, dependsOn: war) {
}
gradle.taskGraph.whenReady { graph ->
if (graph.hasTask(warWithoutGwt)) {
compileGwt.enabled = false
}
}
When I run the Gradle task "warWithoutGWT" while tomcat still runs it says:
C:\Users<myname>\IdeaProjects\NewComApp\build\libs\exploded\NewComApp.war\WEB-INF\classes\com\newcomapp\server\integration\GeoLite2-Country.mmdb (The operation is not applicable to a file with an open area assigned to a user)
I assume that tomcat still holds a reference to that file, and the Gradle task tries to overwrite it (although there was no change to that file). Furthermore, I assume that this kills the rest of the Gradle task so that it does not update the HTML files (it's only an assumption though). How can I arrange an exploded war so that write-protected files are omitted and do not kill the rest of the Gradle task execution?
My answer up to now for this problem is: I changed the gradle script:
task explodedWar(type: Copy) {
into "$buildDir/libs/exploded/${war.archiveFileName.get()}"
exclude "**/*.mmdb"
with war
}
war.dependsOn explodedWar
so I added an "exclude for mmdb files". And this really works.
Is this a correct and good solution or do I overlook something? The reason I am asking is that changing HTML files in the scope of tomcat should be something very common with tomcat based projects. So I wonder if there is a more standardized, easier solution to this? It seems quite clumsy to copy and explode with additional gradle tasks the war file instead of IDEA take care of this.

Related

Errors running builder 'Maven Project Builder' on project

While maven installation, Errors running builder 'Maven Project Builder' on project 'com.oracle.oats'.
Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.6
Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-resources-plugin:jar:2.6
I find the solution in many ways.
This is proxy error. you check access in proxy URL (host) in IE. (Open IE ->TOOL ->INTERNET OPTIONS -> CONNECTIONS -> LAN CONNECTION.)
If it is ok this is working fine.
Otherwise you remove maven-resources-plugin:jar:2.6. Re-execute.
You change eclipse-version.
If any other alternate solution. kindly update

IntelliJ Error:java: java.lang.ExceptionInInitializerError

Every time I encounter this exception in IntelliJ, I fix it trivially and forget the fix easily.
Code:
package whatever;
import org.junit.Test;
public class TestClass
{
#Test
void test() {}
}
Scenario:
Add new TestClass.
Right-click TestClass.
Select "Run 'TestClass'" to run test cases.
The "Messages Build" pane shows:
Information:javac 9-ea was used to compile java sources
Information:Module "dummy" was fully rebuilt due to project configuration/dependencies changes
Information:8/16/17 11:35 PM - Compilation completed with 1 error and 0 warnings in 1s 663ms
Error:java: java.lang.ExceptionInInitializerError
What can possibly go wrong?
What are the likely issues in this simple scenario?
IntelliJ: COMMUNITY 2017.1 (idea-IC-171.4424.56)
To fix the issue, I do:
File -> Project Structure... -> Project Settings / Project -> Project SDK.
Change from "9-ea" to "1.8".
DETAILS
Apparently, the issue is discrepancies in selected JDK-s to build (java 9) and run (java 8).
I'm not sure how "9-ea" gets re-selected there for the same project - neither IntelliJ itself runs in "9-ea" JRE (according to Help -> About) nor JAVA_HOME env var is set to it nor other possible settings (like Maven -> Runner) suggest any "9-ea".
I also didn't manage to run the test under the same JDK (java 9) which it gets compiled under. However, it's unclear what JDK tests are run under because IntelliJ reports only about JDK for compilation.
If you use Lombok: For me it was a solution to set the newest version for my maven lombok dependency in the pom.xml.
*<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.8</version>
</dependency>*
I was facing same error when i tried to run my application in IntelliJ-2019.2 version. Below are the steps i followed to resolve this issue.
Versions:
IntelliJ : IDEA-IntelliJ-2019.2
Java : jdk1.8_221
Go to below path in IntelliJ
File -> Project Structure -> Project -> Project SDK -> (select java version which you want to use )
(In my case under 'project SDK' java-11 was selected, I changed it to 'java8')
Click on 'Apply' and then 'OK'.
I feel I ran into this issue because IntelliJ was trying to compile my java classes using in-built java-11 whereas my java classes are built on java-8. So when i explicitly configured java-8 in IntelliJ, It worked!! Hope this helps.
I started seeing this exception once I installed Java 11 in my machine. JAVA_HOME was by default pointing to Java 11 and my project was still in Java 8. Changing JAVA_HOME to Java 8 jdk fixed the issue for me.
If you have multiple projects each running on a different JDK, use this command to temporarily change the Java version per command.
JAVA_HOME=/path/to/JVM/jdk/Home mvn clean install
If you have recently updated your IDE then you can try these steps.
Delete .idea directory for the idea project/workspace
Then go to File -> Invalidate Caches / Restart...
Once Idea is restarted re-add/import your module(s)
I faced a similar issue with JARs and Jena (while run from IntelliJ it works).
I was using Apache Jena v4.0.0 in my project and have built a JAR (with a main class for the JAR to act as a console app).
The JAR builts successfully with IntelliJ but when run throws java.lang.ExceptionInInitializerError ... Caused by: java.lang.NullPointerException. NPE suggests that something was not initialized properly.
The jar built with previous version Jena 3.17.0 works perfectly.
What I did to fix it
I've opened both the JARs, compared their META-INF folders and encountered the difference in
my.jar\META-INF\services\org.apache.jena.sys.JenaSubsystemLifecycle
The new version (jena v4.0.0) contains only one line:
org.apache.jena.tdb.sys.InitTDB
The old version (jena v3.17.0) contains two different lines:
org.apache.jena.riot.system.InitRIOT
org.apache.jena.sparql.system.InitARQ
I've added the old two lines to the file and repacked new JAR with it:
org.apache.jena.tdb.sys.InitTDB
org.apache.jena.riot.system.InitRIOT
org.apache.jena.sparql.system.InitARQ
It resolved my issue.
Update: recent Jena v4.4.0 builts with the same "bug".
I'm not an expert and there is probably a better way than patching a JAR by hand.
But I still hope that this solution will help someone like me.

IntelliJ run vs running a jar, with a Springboot Kotlin, Multi module Gradle project with Social Oauth2

TL;DR: Why does everything run fine when started via IntelliJ, and why is it broken when call java -jar app.jar. And how do I fix this?
Alright, I have some issues with a backend I am trying to dockerize. I have an application created with Spring Boot (1.4.2.RELEASE) following the Spring Oauth (2.0.12.RELEASE) guide on their page. I follow the Gradle version, since I prefer Gradle over Maven. Also I am using Kotlin instead of Java. Everything is fine, I start via IntelliJ my backend with static front end, I can login via Facebook (and Google and Github), I receive a nice Principal witch holds al the information I need, and I can modify Spring Security to authorize and permit endpoints. So far so good.
Now for the bad part, when I run either ./gradlew clean build app:bootrun or ./gradlew clean build app:jar and run the jar via java -jar (like I will do in my Docker container), my backend comes up. My static front end pops up. Now I want to login via Facebook, I end up on the Facebook login page, I enter my credentials, and... nothing!
I end up back on my homepage, not logged in, no log messages that mean anything to me, just silence. The last thing I see in the log is:Getting user info from: https://graph.facebook.com/me
This Url will give me in my browser:
{
"error": {
"message": "An active access token must be used to query information about the current user.",
"type": "OAuthException",
"code": 2500,
"fbtrace_id": "GV/58H5f4fJ"
}
}
When going to this URL via an IntelliJ start, it will give me credential details. Obviously something is going wrong, but I have no clue what. Especially since a run from IntelliJ works fine. There is some difference between how the jar is started, and how IntelliJ's run config works, but I have no clue where to search for what. I could post trace logging, or all my Gradle files, but perhaps thats too much info to put in 1 question. I will defenitly update this question if someone needs some more details :)
The structure outline of this project is as follows:
root:
- api: is going to be opensourced later, contains rest definitions and DTOs.
- core: contains the meat. Also here is included in the gradle file
spring-boot-starter, -web, -security, spring-security-oauth2, and some jackson stuff.
- rest: contains versioned rest service implementations.
- app: contains angular webjars amongst others, the front end, and
my `#SpringBootApplication`, `#EnableOAuth2Client`
and the impl of `WebSecurityConfigurerAdapter`.
Why does everything run fine when started via IntelliJ, and why is it broken using bootRun or the jar artefact. And how do I fix this?
I found it, the problem was not Multi module Graldle, Spring boot, or Oauth2 related. In fact it was due to a src set config of Gradle, where Java was supposed to be in a Java src set folder, and Kotlin in a Java src set folder:
sourceSets {
main.java.srcDirs += 'src/main/java'
main.kotlin.srcDirs += 'src/main/kotlin'
}
As Will Humphreys stated in his comment above, IntelliJ takes all source sets, and runs the app. However, when building the jar via Gradle, these source sets are stricter. I had a Java file in my Kotlin src set, which is no problem for IntelliJ. But the jar created by Gradle takes into account the source sets as defined in the build.gralde file, which are stricter.
I found my missing bean issue with the code below:
#Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}
The Bean I missed was called AuthenticationController, which is a #RestController, and kinda crucial for my authentication code.

Worklight 6.2 common environment deployment issue since last fix pack : WL is not defined

I recently installed the latest Worklight fix pack update, and am now running v6.2.0.01.20141027-1531
Since that, I've noticed a blocking bug about my common environment deployment on the WL server.
Now when I deploy my app ("Run as > Build all environments" and then "Run on Worklight development server"), running my app in the "Preview as common resources" or in the iPad / Android mobile web environments give me the following Javascript error :
WL is not defined
When I inspect the source code, I realize that the "Worklight generated" Javascript source code is missing in the head tag. I'm supposed to see something like this :
<script>
// Define WL namespace.
var WL = WL ? WL : {};
/**
* WLClient configuration variables.
* Values are injected by the deployer that packs the gadget.
*/
WL.StaticAppProps = {
"APP_DISPLAY_NAME": "appname",
"APP_ID": "project",
"APP_SERVICES_URL": "\/project\/apps\/services\/",
"APP_VERSION": "1.0",
"ENVIRONMENT": "preview",
"LOGIN_DISPLAY_TYPE": "popup",
"LOGIN_POPUP_HEIGHT": 610,
"LOGIN_POPUP_WIDTH": 920,
"PREVIEW_ENVIRONMENT": "common",
"WORKLIGHT_PLATFORM_VERSION": "6.2.0.01.20141027-1531",
"WORKLIGHT_ROOT_URL": "\/project\/apps\/services\/api\/app\/common\/"
};
</script>
...but there's nothing like this in my page's source code (that's why the WL is not found).
Moreover, it does not include the worklight.js and wljq.js files that are supposed to be dynamically added during the build phase (as far as I know). My index.html content is "vanilla", and it explains why nothing works.
So I decided to check in the bin archives that are generated during the build phase. It happens that the app-common.wlapp archive contains the correctly generated source code, with all the Worklight generated stuff inside. It looks like something wrong is happening during the deployment step. I tried to import it manually in the WL Console, but it's unfortunately not working either (same result).
I did not manage to reproduce the problem creating a new project, it looks like it only happens after an update of Worklight to the latest fix pack version. Thus, it's complicated to provide a sample project.
NB : everything works fine once running on an actual device. Only the common environment is broken in my case. But a colleague of mine also noticed this issue for the other environments so... I'll let you know if I manage to reproduce it for the other environments.
Thank you for you help, please ask if you need more inputs from me.
You are not required to do "build all..." and then "run on...". That's just repeating the same step, as "Run on Worklight Development Server" means "build the project artifacts (.wlapp file, .war file) and then deploy them".
A vanilla index.html contains references to initoptions.js, main.js, main.css and messeages.js. You are kinda confusing in your wording as to what is expected and where is it expected.
Anyway, try the following:
Close Eclipse
Go to your tmp folder and delete the wlBuildResources folder
Open Eclipse and try the following two paths:
Right-click the app folder > Run As > Run on Worklight Developer Server > Open Worklight Console > preview as common web resources
Right-click the app folder > Run As > Preview
Tell me which of the two fails, if at all.

Android Studio: How to attach javadoc

It might be very trivial question, But I couldn't find any option to attach javadoc/source with local jar dependencies (in libs folder) in android project. I can't believe I have spent a hour on such simple stuff :(
Google search result just tells about adding android documentation or adding javadoc in eclipse. That's not What I am looking for!
I found a solution just now since I can't find any other solution for a long time.
suppose:
your lib name is: libxxx.jar
javadoc name is docs.zip
under folder .idea/libraries, you can find libxxx.xml. replace JAVADOC with
<JAVADOC>
<root url="jar://C:/yourpath/doc.zip!/" />
</JAVADOC>
then ctrl+alt+y to sync the project. (Don't do "Sync Project with Gradle files", it will delete the changes)
I've found & tested a way in which we can add the javadocs without loosing them after a gradle sync.
Right click in Project panel > External libraries > lib-name
Click Library properties
Click specify documentation URL
And specify an URL. For my machine it was file:///opt/android-sdk/extras/google/google_play_services/docs/reference
The result was:
And after a project sync I could browse the javadoc of the google play services
Note:
This placed the same JAVADOC entry in the xml identified by Jason but doesn't disappear after doing a sync with gradle
It is possible:
Open Project structure
Navigate to dependency that you want
Press button from the screenshot and provide folder or file
Another way to attach sources:
Step into class from library (Hover mouse over class name and do CMD + Left click or CMD + B)
You will see decompiled version of class there menu on the top right side of editor "Attach sources.."
I had problem with answer from Matyas that I wasn't seeing my local .jar library in the External Libraries list, because it only show maven attached libs.
Solution is mentioned here: https://code.google.com/p/android/issues/detail?id=73087#c26
Right click on the "Structure" tab and check "Split Mode" (so you can open both "Project" and "Structure" tabs at same time).
Open both "Project" and "Structure" tabs at the same time.
In "Project" tab select "Android" perspective and then select your module folder (e.g. "app")
In "Structure" window you should now see list of all libraries, including your local *.jar
Continue as in answer from Matyas:
Right click on wanted library and select "Library Properties..."
If you have *.jar with javadocs locally, you can press "add" button (green "+") and search for the file on your disk (you don't have to type "file://" path manually).
Still a current issue as of my posting it seems:
https://code.google.com/p/android/issues/detail?id=73087
After some investigation, I stumbled upon this:
https://github.com/xujiaao/AARLinkSources
Works like magic!
I wasted so much time on this too...
Here's a gradle task which finds source and javadoc by location/naming convention, and registers them in the .idea files on sync. It belongs in the root gradle file's allProjects section. As-is, it expects to find [projectname]/libs/lib.jar next to lib-sources.jar and/or lib-javadoc.jar. Also, as noted in comments, if your javadocs not pathed at "/" inside the jar, you may need to change the script to add "docs/html" (for example) at the end of "jar://$doc!/".
allprojects {
task addJavaDoc {
afterEvaluate {
// Specify paths, this will be run per non-root project
def projectDir = project.getProjectDir().getCanonicalPath()
def rootDir = project.getRootDir().getCanonicalPath()
def lib = projectDir + '/libs'
// println lib // Uncomment this to troubleshoot
// Get any jar dependencies register in the lib folder
fileTree(include: ['*.jar'], exclude: ['*-source.jar', '*-javadoc.jar'], dir: lib ).each { File jar ->
def jarName = jar.getName()
def moduleName = jarName.substring(0, jarName.lastIndexOf("."))
// IntelliJ does this to file names when making the xml files
def escapedName = moduleName.replace("-", "_").replace(".", "_")
def xmlFile = "$rootDir/.idea/libraries/${escapedName}.xml"
// println xmlFile // Uncomment this to troubleshoot
if (new File(xmlFile).exists()) {
['javadoc', 'sources'].each {String docType ->
// Get sources or java doc by naming convention, (expects name-sources or name-javadoc
def doc = "$lib/$moduleName-${docType}.jar"
// println doc // Uncomment this to troubleshoot
if(new File(doc).exists()) {
def xml = new XmlParser().parse(xmlFile);
def xmlTag = docType.toUpperCase()
// Perform xml replacement by convention
xml.library[xmlTag].replaceNode {
"$xmlTag" {
root(url: "jar://$doc!/")
}
}
// Write out changes
new XmlNodePrinter(new PrintWriter(new FileWriter(xmlFile))).print(xml)
// Notify that changes worked
println "Fixed up reference to $doc"
}
}
}
}
}
}
}
Also, if you are using jcenter or mavencentral, javadocs and sources should work for downloaded jars without using that task, but you may have to add this in each non-root gradle file:
apply plugin: 'idea'
idea{
module {
downloadJavadoc = true
downloadSources = true
}
}
Personally tested successfully!
1.Project Structure(ctrl+alt+shift+s)
2.SDK Location
3.JDK Location
4.UnCheck "Use embedded JDK(recommended)"
5.Select your own jdk path(My Path:C:\Program Files\Java\jdk1.8.0_111)
6.Synchronized(ctrl+alt+y)
success
in android studio if you use compileSdkVersion 23 in buidl.gradle and you have downloaded the SDK 23 document file and source file in SDK manager , the android API and java API doc and source will all show auto, you don't need to manual set.
There is a solution, This procedure take place through terminal ,
I have tested solution in MAC OS.
1) Move to your project folder
2) ls -al (to show hidden files)
3) Move to .idea folder , Command : cd .idea
4) Move to libraries folder , Command : cd libraries/
5) Now you can see list of all xml files for your libs or jars. Edit it like , vi open androidasync_2_1_7.xml
6) In the editor screen ,
For inserting
Press i
Now you see <SOURCES /> tag we have to provide a path here like,
<SOURCES>
<root url="file://$PROJECT_DIR$/androidasync/src/main/java" />
</SOURCES>
For exiting
Press Esc
:wq //for exiting and saving
:q! //for exiting without saving
7) Restart Android studio (Sometime it needed also sync gradle).