Thursday, May 22, 2014

Another shitty day with maven, m2e and eclipse

Please, don't get the idea from the title that I don't like java. It's quiet the contrary; however, sometimes one does get  little annoyed when it's not possible to be productive because one has to constantly trouble shoot little issues, which to say is at least possible with java because of its open source nature. Actually, these "little issues" are rarely related to the core java technology but the trendy stuff which pops up and is programmed with java and everybody is expected to use.

With that said, all I wanted to do is snoop/debug around in the spring security classes a little. I already have some projects (both mavenized and not mavenized in both netbeans and eclipse), which are working, but I wanted to start from scratch and do everything with eclipse kepler and not STS, which I thought would be easy. 

So I started and 2 days later I finally had it. Yes, I let myself be distracted by unnecessarily delving into code, which I didn't need to delve into, and read a lot about maven; it was all very interesting but probably not so productive.

First, I generated a basic web application using the maven archetype: "maven-archetype-webapp" as shown here:

mvn archetype:generate -DgroupId={your-groupid} -DartifactId={your-project-name} -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

(See here for yesterday's trials and tribulations).

Next, I imported the newly created project into Eclipse and added the necessary Spring dependencies to the pom so that it looked like this:


So far so good. 

Then I thought I could setup tomcat up in Eclipse and run my newly imported project from within eclipse as shown below:


This works as long as there are no dependencies beyond those provided by the j2ee environment; however, with Spring it wouldn't work. I always got a 404, which was obvious because I could see that the Spring  libraries weren't be deployed into the folder specified by the wtp.deploy JVM argument.

So I screwed around for a couple of hours playing with the project's facets. I tried to get the "Dynamic Web Module" facet (see red ellipse below)  set to version 3.0, which, in the end, was a real adventure. See here about how do it.

http://stackoverflow.com/questions/18122336/cannot-change-version-of-project-facet-dynamic-web-module-to-3-0


Once I finally got it set to version 3.0, then it was just as hard to get the "Further configuration available..." option to appear (See red below) at the bottom of the facet's screen dialog.


It finally appeared after I tinkered around with the Java version number and closed the dialog and reopened it a few times.

Anyway, all the work with the facets was a waste of time and I knew it because I was straddling the grey area between maven and eclipse. The thing to do, which I originally wanted to avoid, was to use the maven-tomcat-plugin, which didn't seamlessly work either.

<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>

</plugin> 

Now, begins a new series of trials and tribulations; however, this time with the maven-tomcat-plugin. The first error was:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project mycrazyapp: Compilation failure
[ERROR]/C:/Projekte/spring/workspaces/blogws/mycrazyapp/src/main/java/org/springframework/security/samples/config/SecurityConfig.java:[10,8] cannot access javax.servlet.Filter
[ERROR] class file for javax.servlet.Filter not found

I tried to add in necessary dependency using the m2e plugin but got this error:


See here:
http://stackoverflow.com/questions/18047843/cannot-search-for-artifact-in-eclipse-kepler-using-m2e-plugin

The above fix didn't work for me, so I just added it in manually.

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>compile</scope>
</dependency>

The addition of the servlet-api jar caused this:

java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardCon
text[/mycrazyapp]]

Caused by: java.lang.LinkageError: loader constraint violation: loader (instance of org/apache/catalina/loader/WebappClassLoader) previously initiated loading for a d
ifferent type with name "javax/servlet/ServletContext"

Which was fixed by changing my pom dependency declaration's scope of the servlet-api to" provided".

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>


Ran maven again.

mvn tomcat7:run


Ok, almos there. Here, I thought that maybe I needed to change the maven-compiler-plugin configuration to include debugging information. I looked but it's default is to include debugging information.

This is similiar but the reverse of what I need.


Found this, so I added it; however, that still didn't fix it.


<configuration>
               <source>1.7</source>
               <target>1.7</target>
               <debug>true</debug>
</configuration>
I screwed around for a while and tried miscellaneous settings within eclipse even though I knew eclipse wasn't responsible for the compiling.

Eventually, I got the idea to do this:

mvnDebug tomcat7:run
Preparing to Execute Maven in Debug Mode
Listening for transport dt_socket at address: 8000

from here:

And Voila!


But I still got the error about "line numbers" before stepping into my source code. I think it's because the debugger first stops in the  dynamically generated cglib proxy code; however, it 's tolerable.

No comments:

Post a Comment