Eclipse - How to add Kotlin to a Tomcat Project - Compile Kotlin AND Java Files - eclipse

I need help adding Kotlin compiling to an existing eclipse java tomcat project.
For the record I am on Eclipse Oxygen 4.7, and have installed the Kotlin plugin. I have successfully created and ran gradle based spring boot Kotlin rest api demo and can compile and run "Kotlin Only Projects" and use my other java libs - AWESOME.
However now that I have Kotlin I want to use it in work for my other 'legacy' Tomcat 8 projects that are Java J2EE - Dynamic WTP ... new servlets I want to write in Kotlin.
I want to be able to add a Kotlin class in the java src folder in any of my previous packages so when I build a war it all logically together.
The IDE seems ok with this - I created a Kotlin class in a package (in a tomcat project java src package folder) and it have no issues on resolving, imports, or dependency...
I was thinking WOW!, Ok now to restart the Tomcat server in WTP and I'll be able to execute my Kotlin Servlet. No - 404 404 404.
I inspected the WEB-INF/classes build folder and along all the Java class files... guess what I saw?! A file called KotlinServlet.kt ( thats the name of my test servlet, the source version not a compile class version)
So for some reason the build /package/ deployment did not bother to compile the *.kt file and just copied it over (maybe that is the default for general WTP deploy).
I wonder if this would be solved by "Add Kotlin Nature" , the strange part is that none of the eclipse Kotlin menu functions are available to me. If I right click my project I get no Kotlin menu options.
Eclipse states the plugin is installed.
So my question is:
Given an existing conventional WTP Tomcat servlet project, HOW can we get it to identify and a compile *.kt files? How and why do I not have the Kotlin menu functions? (I downloaded and tested both the Oxygen Java and J2EE version - installed the Kotlin plugin - and no menus there either to add a Kotlin Nature. I also then tried the nightly build of the plugin. No menus appeared. Obviously the puglin is installed compiling other Kotlin projects and works in the editor.
From my research - "Add Kotlin Nature" is supposed to do the trick for joint Java / Kotlin compiling but that menu function is not available anywhere???
If this is a "Kotlin Project" eclipse compiles Kotlin fine.
Any Ideas? Seems like an IntelliJ conspiracy.
Thanks,

You need add correspond dependencies. The easiest way to manage project dependencies is to use build tools (maven, gradle, Ant+Ivy...). In maven example you need:
<properties>
<!-- your properties... and define version fir kotlin -->
<kotlin.version>1.1.4</kotlin.version>
</properties>
<dependencies>
<!-- your project dependencies... and add one for kotlin -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jre8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- your project plugins... and add one for kotlin -->
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
</plugins>
</build>
If you use IDE you need to install kotlin language support plugin to make kotlin compile or test run more user friendly.

OK I am narrowing in on this. I found that the kotlin classes are being compiled to this location under runtime lib? Maybe this will be something I can figure to build to my normal classes folder.

OK - To get all the Kotlin menus - turns out you need to be on PACKAGE EXPLORER tab not PROJECT EXPLORER . arggh just found that by fluke.
The creation of a .kt file will automatically add the runtime and libs for Kotlin to your project.
Now on the the issue of the build / compile now. Still outstanding.

OK Update - Downloaded IntelliJ, and "IT" works out of the box, in 15 min I had a Hybrid Java/Kotlin Tomcat Project running.
Still Interested in the Eclipse solution but I am probably just going to shell out the cash for IntelliJ. Time is money.

Related

Eclipse Project can't find Java 13 classes after conversion to Maven

I have an old project in Eclipse that runs fine with Java 13. I'm using Eclipse 2019-09 (4.13.0), I have the Java 13 JRE on the build path and as the default JRE, the compliance level set to 13, and the Eclipse Java 13 patch installed. No problems.
When I convert this to a Maven project, some of the JRE classes are no longer found. BitArray (found in java.xml com.sun.org.apache.xalan.internal.xsltc.dom), DocumentImpl (found in java.xml com.sun.org.apache.xerces.internal.dom), BevelBorder (found in java.desktop com.sun.java.swing.plaf.motif.MotifBorders) and others.
If I hover over one of the fields - say BitArray - Eclipse will prompt me to Import 'BitArray' (com.sun.org.apache.xalan.internal.xsltc.dom), but when I do I simply get an error saying The import com.sun.org.apache.xalan cannot be resolved
I have the following lines in pom.xml:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>13</release>
</configuration>
</plugin>
I've done many Clean&Builds and Maven updates. I've tried changing the order in the build path (read that was a bug in earlier releases). I've even used the Hello World Maven project example with the same result - as soon as I add BitArray, it fails.
Any suggestions would be greatly appreciated. I've been googling this for half a day now, and still haven't found the solution.

m2e error in MavenArchiver.getManifest()

I am seeing an error in my STS and am not sure how to debug it. Searching around I only see vague references to the error and no solutions.
The error is:
org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration) pom.xml /<maven projectName> line 1 Maven Configuration Problem
Question: Can anyone give any suggestions on how to get more information on the issue in order to debug it further or any possible solutions?
Maybe some more context on what the functionality of method is. The javadoc associated with the MavenArchiver.getManifest() method is not very detailed and there is no stack trace that I see. I've tried several refreshes and updates of the project and associated projects including clearing my local m2 repo.
STS info:
Version: 3.7.3.RELEASE
Build Id: 201602250940
Platform: Eclipse Mars.2 (4.5.2)
m2e info:
Version: 1.6.2.20150902-0002
I encountered the same issue after updating the maven-jar-plugin to its latest version (at the time of writing), 3.0.2.
Eclipse 4.5.2 started flagging the pom.xml file with the org.apache.maven.archiver.MavenArchiver.getManifest error and a Maven > Update Project.. would not fix it.
Easy solution: downgrade to 2.6 version
Indeed a possible solution is to get back to version 2.6, a further update of the project would then remove any error. However, that's not the ideal scenario and a better solution is possible: update the m2e extensions (Eclipse Maven integration).
Better solution: update Eclipse m2e extensions
From Help > Install New Software.., add a new repository (via the Add.. option), pointing to the following URL:
https://repo1.maven.org/maven2/.m2e/connectors/m2eclipse-mavenarchiver/0.17.2/N/LATEST/
Then follow the update wizard as usual. Eclipse would then require a restart. Afterwards, a further Update Project.. on the concerned Maven project would remove any error and your Maven build could then enjoy the benefit of the latest maven-jar-plugin version.
Additonal notes
The reason for this issue is that from version 3.0.0 on, the concerned component, the maven-archiver and the related plexus-archiver has been upgraded to newer versions, breaking internal usages (via reflections) of the m2e integration in Eclipse. The only solution is then to properly update Eclipse, as described above.
Also note: while Eclipse would initially report errors, the Maven build (e.g. from command line) would keep on working perfectly, this issue is only related to the Eclipse-Maven integration, that is, to the IDE.
I found my answer! I looked into the pom for any plugins that have a dependency on the maven-archiver and found the maven-jar-plugin does. It was using the latest 3.0.0 version. When I downgraded to 2.6 it seems to fix the issue :-)
I had the same problem with a spring boot project. the solution was to downgrade the jar maven-jar-plugin from 3.2 to 2.6 . i had just to add this to the project pom:
<properties>
<maven-jar-plugin.version>2.6</maven-jar-plugin.version>
</properties>
I had exactly the same problem. My environment was:
Spring STS 3.7.3.RELEASE
Build Id: 201602250940
Platform: Eclipse Mars.2 (4.5.2)
The symptoms of the problems were:
There was a red error flag on my PM file. and the description of the error was as described in the original question asked here.
There were known compilation problems in the several Java files in the project, but eclipse still was not showing them flagged as error in the editor pane as well as the project explorer tree on the left side.
The solution (described above) about updating the m2e extensions worked for me.
Better solution (my recommondation):
update Eclipse m2e extensions
From Help > Install New Software.., add a new repository (via the Add.. option), pointing to the following URL: https://otto.takari.io/content/sites/m2e.extras/m2eclipse-mavenarchiver/0.17.2/N/LATEST/
Select the m2e extensions, accept the license.
After update, you will be asked for restarting STS. The problem goes away after STS comes back up.
I had the same problem with a spring boot project. The solution was to downgrade the spring-boot-starter-parent dependency version from 2.0.0.RELEASE to 1.5.10.RELEASE(you can move to any stable version)
from:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
to
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
I had also faced the same issue and it got resolved by changing the version from 3.2.0 to 2.6 as shown in below pom.xml snippet
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<warName>Spring4MVC</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
Upgrade your m2e extensions instead downgrade.
From Help > Install New Software.., add a new repository (via the Add.. option)
Specify name for your plugin and add path http://repo1.maven.org/maven2/.m2e/connectors/m2eclipse-mavenarchiver/0.17.2/N/LATEST/
once you are done with installation, please restart eclipse and update your project.
I also faced the similar issues, changing the version from 2.0.0.RELEASE to 1.5.10.RELEASE worked for me, please try it before downgrading the maven version
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
I solved this error in pom.xml by adding the below code
spring-rest-demo
org.apache.maven.plugins
maven-war-plugin
2.6
I had also faced the same issue and it got resolved by commenting the version element in POM.xml as show.
org.apache.maven.archiver.[MavenArchiver](https://maven.apache.org/shared/maven-archiver/apidocs/org/apache/maven/archiver/MavenArchiver.html).getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration)
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<!-- <version>3.5.1</version> -->
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<!-- <version>3.1.0</version> -->
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
Use the steps given in this link. It worked for me.
Step - 1 Right click on your project in Eclipse
Step - 2 Click Properties
Step - 3 Select Maven in the left hand side list.
Step - 4 You will notice "pom.xml" in the Active Maven Profiles text box on the right hand side. Clear it and click Apply.
Step - 5 Run As -> Maven clean -> Maven Install
Hope it helps!

How to run the project after building with maven

I am new to maven. So I have a project with pom.xml file. So I ran that with maven and the build was successful. I have glassfish. Glassfish is already running separately. So now what is the next step to run the project with Glassfish? My IDE is eclipse.
You have to first tell Maven to build the WAR, check out this plugin for that: http://maven.apache.org/plugins/maven-war-plugin/.
Then you need to tell maven how to deploy to glassfish, you can either configure a Maven execution plugin to do this (see here: https://www.mojohaus.org/exec-maven-plugin/). Or you can look around for a custom plugin devoted to integrating maven with glassfish. This one looks promising, but I have not used it: http://maven-glassfish-plugin.java.net/.
Maven provides a lot of basic functionality out of the box, but most of the cooler stuff with build automation is done through plugins.
Update
Just updating to add a very simple Pom that will do a auto-deployment. Note: if you just run a "mvn clean install", with the packaging set to 'war', maven will build the .war file for you and place it in the target/ folder. You can take this and deploy it to glassfish manually if you just want to get started.
Below is part of a very simple pom that uses the Maven execution plugin to auto-deploy to glassfish as a function of the build:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
<phase>install</phase>
</execution>
</executions>
<configuration>
<executable>${path-to-asadmin-util}</executable>
<arguments>
<argument>deploy</argument>
<argument>--user=${username}]</argument>
<argument>--passwordfile=${password-file}</argument>
<argument>--host=localhost</argument>
<argument>--port=4848</argument>
<argument>target/${project.name}</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
This basically just calls the deploy command on the glassfish asadmin utility[1]. You need to fill in the following variables:
${path-to-asadmin-util} --> this is the path to your asadmin utility
(normally in the glassfish_home/bin)
${username} --> glassfish admin username
${password-file} --> password file for logging into glassfish
admin[2]
${project.name} --> name of your war
If you want to get more complicated I suggest taking a look at this thread: GlassFish v3 and glassfish-maven-plugin (Mac).
[1] - http://docs.oracle.com/cd/E18930_01/html/821-2433/deploy-1.html#SJSASEEREFMANdeploy-1
[2] - http://docs.oracle.com/cd/E18930_01/html/821-2435/ghgrp.html#ghytn
Additonnaly, you should have a glance at this StackOverflow thread, dealing with maven deployement in glassifsh : https://stackoverflow.com/a/1836691/1047365.
For further understanding of Maven, you should REALLY read this (free) book : http://www.sonatype.com/books/mvnref-book/reference/. This is THE reference for Maven.
We can explain you what Maven is doing, producing, etc ... but Sonatype made a great work and you'll probably learn more reading it than we could ever do !
Regards.
I found this tutorial useful: http://tshikatshikaaa.blogspot.com/2012/05/introduction-to-maven-concepts-crash.html

Eclipse WTP not publishing Maven dependencies

I'm trying to set up a basic hello world project using Eclipse Indigo and a Tomcat server. I created a dynamic project with a simple servlet. Tested the servlet and that worked fine. Then I enabled Maven support and added logback to my pom. I put a logging statement in the servlet's doGet method. When running the servlet, it complains it cannot find any bindings because the logback jars are not being copied into the Eclipse tomcat instance. I expected to find the jars published somewhere in here:
${workspace}\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\
How do I get Eclipse to work with WTP/Maven properly? I also tried installing the m2e-wtp connector with no difference.
Check Deployment Assembly (context menu on project), there must be mapping Maven Dependencies -> WEB-INF/lib.
Coming from an ASP.NET background, I find it shocking how much work it takes to get a webapp running with Eclipse WTP and Maven especially if you are learning on your own. Hopefully this quick start guide will help someone else get up to speed quickly.
There are two ways to get a hello world project working in Eclipse WTP with Maven. You can create a Dynamic web project and then add the Maven nature or you can do the opposite.
Pre-requisites for Eclipse with update sites
"Web, XML, Java EE and OSGi Enterprise Development"
http://download.eclipse.org/releases/indigo
"Maven Integration For Eclipse"
http://download.jboss.org/jbosstools/updates/m2eclipse-wtp/
"Maven Integration for WTP"
http://download.jboss.org/jbosstools/updates/m2eclipse-wtp/
Startup configuration
Install copy of Tomcat 7 from http://tomcat.apache.org/download-70.cgi
Window -> Preferences -> Server -> Runtime Environment
Add Apache Tomcat 7.0 and select local installation directory
Option 1: Create Dynamic Web Project then add Maven Nature
Create new Maven project, select archetype
org.apache.maven.archetypes:maven-archetype-webapp
Change to Java EE perspective.
Create a new source folder, src\main\java. Notice how Eclipse is not smart enough to do this for you and also the ordering of the folders is incorrect.
src\main\java folder is listed after src\main\resources. This can be manually fixed later in the project properties.
Create a new servlet. Notice how Eclipse defaults this file in the wrong folder src\main\resources because the order is wrong. Instead, manually select src\main\java.
Change the URL mapping on the second page of the wizard to /* to make testing easier.
Now our servlet is ready but the dependencies on the servlet api are unbound. A) we can add the servlet api as a dependency to our project or B) we can bind to the Eclipse server config for Apache 7.0.
For option A, add this dependency to the pom:
.
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-api</artifactId>
<version>7.0.${set this}</version>
<scope>provided</scope>
</dependency>
For option B:
Project properties -> Java Build Path -> Libraries -> Add Library -> Server Runtime -> Apache Tomcat 7.0
Right click and run on server:
A blank page should come up in the internal browser like http://localhost:8080/${artifact}
Test of dependency publishing:
Add joda-time to the pom.
Add this line in the servlet created earlier for the doGet method and import the necessary dependencies:
.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println("The time is now: " + new DateTime().toString());
}
Reload the test page and the output should now be:
The time is now: 2012-03-03T14:14:29.890-05:00
Now if you want to play with Servlet 3.0 and annotations this is not enabled by default, for what reason I don't know.
First force Maven to use Java 1.6 by adding this to your pom, otherwise each time you update your pom the configuration will revert to Java 1.5.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
Open Project Properties -> Project Facets. Change the Version under "Dynamic Web Module" to 3.0, change java version 1.6
Create a new Servlet with class name AnnotatedServlet in src\main\java and notice how the #WebServlet annotation is auto created.
Option 2: Create Dynamic Web Project then add Maven Nature
Select Tomcat Runtime and Dynamic Module Version 3.0
Create source folder src\main\java
Set default output target\classes
Set context directory src\main\webapp
Check generate web.xml
Create servlet with mapping /* for quick testing
Add an output statement to the doGet method
response.getWriter().println("Another test");
Double click the "Deployment descriptor" and add this attribute to the root web-app element metadata-complete="false"
Right click project and select Run As -> Run On Server
Right click project -> Configure -> Convert To Maven Project
Select packaging as war
Edit pom and set compiler to use java 1.6 and add joda-time dependency:
.
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
Right click on the web project in Project Explorer then choose Maven -> Update Project
I faced a similar problem and although I had configured Deployment Assembly correctly it still didn't work. Then I found that under Window -> Preferences -> My Eclipse -> Java Enterprise Project -> Web Project, under the Deployment tab, the management of Dependent projects was turned off. I changed it to deploy jars of dependent projects to the lib folder and then everything worked like a charm. I could even turn off the Deployment Assembly option.

How do I develop a maven plugin using Groovy in Eclipse

I want to write a Maven plugin in Groovy, but under Eclipse (Galileo).
I've downloaded and installed Groovy-Eclipse plugin
I've created my very simple POM file (included below).
I've created a simple Echo mojo and place it under "/src/main/groovy/com/acme/maven/plugins/foo".
I performed "Import Existing Maven Project" in Eclipse (using M2Eclipse plugin).
The problem is that I don't see "src/main/groovy" as a source folder, which makes it hard to develop:
- I have to create the package directory structure (com/acme/maven/plugins/foo) manually
- Refactoring probably won't work easily
- Incremental probably won't work.
How do you guys develop your Maven plugins using Groovy in Eclipse?
You should definitvly install the Groovy-Eclipse plugin if you plan to develop Groovy code in Eclipse. You can find all the details here:
http://groovy.codehaus.org/Eclipse+Plugin
You should also add the Groovy Maven Plugin to your pom.xml like this (see this page for details):
<build>
<plugins>
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Now update your Eclipse project configuration via Right-click on project -> Maven -> Update project configuration. Now you should see that the src/main/groovy folder has been added to the source folders.
Use GMaven and the eclipse plugin as suggested by chkal.
In addition to GMaven, and Groovy-Eclipse, there is m2eclipse support for Groovy-Eclipse that should be installed separately. Go to the Groovy-Eclipse snapshot update site:
http://dist.codehaus.org/groovy/distributions/greclipse/snapshot/e3.6/
And install the feature from there. This feature will ensure that your groovy/maven projects are set up correctly when they are imported.