0

I have a pretty straight forward servlet running in Tomcat, which roughly starts like this

import javax.ws.rs.ApplicationPath;

import org.glassfish.jersey.server.ResourceConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import security.AuthenticationFilter;

@ApplicationPath("/")
public class MyMain extends ResourceConfig {

    private final static Logger logger = LoggerFactory.getLogger(MyMain.class);

    private Map<String,Map<String,String>> myConfig = null;

    public MyMain() {

        final String myEnv = System.getProperty("myEnvironment");

        // doing more stuff, registering resources ...
    }
}

The project runs locally in my Eclipse, which also starts and stops Tomcat. There is a Server beside my project.

But the server (Tomcat) does not start the servlet

I have checked my server and deploy path against the run configuration in Eclipse and they should match.

The pom.xml looks like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>sustainableDataPlatform.org</groupId>
  <artifactId>my-project</artifactId>
  <!-- <version>0.1.1</version>-->
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>MY PROJECT</name>
  
  <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <version.jersey>2.32</version.jersey>
  </properties>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <release>11</release>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.3</version>
        <configuration>
        <!-- https://stackoverflow.com/a/33390519/2092322 annotations instead of web.xml -->
        <failOnMissingWebXml>false</failOnMissingWebXml>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
            </manifest>
          </archive>
          <warName>my-project</warName>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
        <!-- https://mvnrepository.com/artifact/javax.ws.rs/javax.ws.rs-api -->
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>${version.jersey}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${version.jersey}</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${version.jersey}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>${version.jersey}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.14.0</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.14.0</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.14.0</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>2.14.0</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20151123</version>
        </dependency>

        <dependency>
            <groupId>org.neo4j.driver</groupId>
            <artifactId>neo4j-java-driver</artifactId>
            <version>4.2.0</version>
        </dependency>
        <!-- maybe use jakarta mail? javax.mail 1.4.x? -->
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.ws</groupId>
            <artifactId>jaxws-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.4.1</version>
        </dependency>
  </dependencies>
</project>

Server Configuration in Eclipse and its results

Server Location of Eclipse server

# server.xml in tmp folder after starting the server (*run on server*)
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
    <Context docBase="/home/myname/work/sdp/eclipse/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/webapps/ROOT" path="" reloadable="false"/>
    <Context docBase="sdp-api" path="/sdp-api" reloadable="true" source="org.eclipse.jst.jee.server:sdp-api"></Context>
</Host>

# same part of server.xml in Server folder of server project in Eclipse after having the servlet project configured for (added to) server 
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
    <Context docBase="sdp-api" path="/sdp-api" reloadable="true" source="org.eclipse.jst.jee.server:sdp-api"/>
</Host>

Targeted Runtime of the servlet project: Tomcat

Runtime

while I do not get 'Runtime Composition'. What is that?

Project Build path

Sources

And what is still confusing me is the order part of the build path config, maybe I am doing something wrong here.

build path order

Run Configuration Classpath

Old

I have adjusted the execution environment and ideally the server has a reference to the servlet project, not the other way round

Now, when I export the project in Eclipse, I am getting a fine my-project.war. But when I run mvn clean package the server throws errors and when I try to start it again, Eclipse yells at me:

Publishing failed with multiple errors
File not found: /path/to/my-project/target/m2e-wtp/web-resources/META-INF/MANIFEST.MF.
File not found: /path/to/my-project/target/m2e-wtp/web-resources/META-INF/MANIFEST.MF.
File not found: /path/to/my-project/target/m2e-wtp/web-resources/META-INF/maven/mainPackage/my-project/pom.properties.
File not found: /path/to/my-project/target/m2e-wtp/web-resources/META-INF/maven/mainPackage/my-project/pom.properties.
File not found: /path/to/my-project/target/m2e-wtp/web-resources/META-INF/maven/mainPackage/my-project/pom.xml.
File not found: /path/to/my-project/target/m2e-wtp/web-resources/META-INF/maven/mainPackage/my-project/pom.xml.

So I need to:

  1. decouple the project from the Tomcat Server
  2. When I just run it again on the Server, the servlet is not running, so I need to bind the project back to the Server in Eclipse by using the reference:

enter image description here

This changes .classpath:

- <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/java-11-oracle">
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">

and

- <attribute name="owner.project.facets" value="java"/>
+ <attribute name="maven.pomderived" value="true"/>*

First question: what does this mean?

.project is of course also changed:

  <projects>
+    <project>Servers</project>
  </projects>

I am pretty sure that I have a wrong configuration somewhere and I would like to use mvn besides Eclipse. But what exactly am I doing wrong?

Any more config stuff needed?

BairDev
  • 2,865
  • 4
  • 27
  • 50
  • 2
    Very short answer. The truth is in the pom's. M2E creates the information from the POM...also why not following the convention and changing the default via `WebContent`... put stuff into `src/main/webapp`... – khmarbaise Apr 08 '21 at 15:21
  • 1
    The `.classpath` change is to the abstracted Execution Environment reference to JavaSE-11; it's more portable and what you should use anyway. The `.project` change is because you modified the Project References, which does nothing useful here. You should go to the Server property page to actually affect which server is used to run the project using Eclipse's "Run/Debug on Server" abilities. – nitind Apr 08 '21 at 16:05
  • Does your Eclipse recognize that the project is a Maven project? How did you import it? If it does, then your pom.xml should be read by Eclipse to determine a lot of the project settings. If not, this could be the cause of many problems. I'm also not sure about associating the project with the server from the properties menu. I usually just right click on the server in Eclipse's servers view/tab and click "Add/Remove application" or something like that. – admdev Apr 13 '21 at 19:20
  • @admdev The project has been changed to a maven project from begin on, so the *pom.xml* is reflected in eclipse settings (Project -> Settings -> Maven -> Update Project). I still have no idea, why the servlet is not started in Tomcat. Just Tomcat runs fine. (I am using 'src/main/webapp' instead of 'WebContents' and the std Java *JavaSE-11* execution environment. – BairDev Apr 13 '21 at 20:11
  • @nitind It does not matter, whether I *reference* the server in my servlet project or the other way round, Tomcat runs fine, but the servlet is not recognized/started at all. – BairDev Apr 13 '21 at 20:14
  • We can't see that. – nitind Apr 13 '21 at 20:16
  • @nitind Well, there is not much to see. That's the problem. I am using *Use workspace metadata* as Server Location, a temporal server path and some custom deploy path. But I don't know whether this can affect the cooperation of Tomcat and Eclipse. What else could be of interest? – BairDev Apr 13 '21 at 20:38
  • What's the Servers view show? Is the project associated to the server instance? – nitind Apr 13 '21 at 20:39
  • It is associated, in the server view I can see the server and the servlet (project) below, which is listed in the *configured* resources of the server. The server is shown in the 'Server' tab of the properties of the project, Tomcat is also listed as 'Targeted Runtime' in the project. (I have updated the question with an image of this.) – BairDev Apr 13 '21 at 21:07
  • Tomcat runs but without the servlet running this sounds to me like the project wasn't added properly to the server. It would be of great help to post screenshots of your server view with the Tomcat expanded to see what's under it. – admdev Apr 14 '21 at 04:34
  • Also post the full pom.xml. Usually when Eclipse imports a Maven project, it takes care of the facets and all of the necessary settings such as the Java version from the pom.xml. If you have to fiddle with those settings in Eclipse, it is possible that your pom.xml isn't configured correctly. Does it have the packaging set to `WAR`? Are you adding the the `servelt` jar to your dependencies? Your project seems to be a straight forward servlet project, and the more information you provide the easier it will be to spot where the mistake is. Also how did you add the server to Eclipse? – admdev Apr 14 '21 at 04:43
  • @admdev Thanks for your effort! I have added the pom.xml and some screenshots, also from the server. It looks right to me and looked the same way when it worked. *Packaging* is set to `war` in pom.xml. What do you mean by "Are you adding the the servelt jar to your dependencies?" Dependencies of what? I have added the build path config and classpath of the run configuration, but maybe you are talking about something I am missing? – BairDev Apr 14 '21 at 08:10
  • @admdev The server has been added to the server with the usual wizard. Its configuration looks like this: https://www.baeldung.com/eclipse-tomcat#configure Generally speaking: Eclipse copies Tomcat and its configuration to a tmp folder, including the servlet and everything should run there, right? There could be something wrong in my run configurations, because all paths (server, deploy) do match? – BairDev Apr 14 '21 at 08:21
  • @admdev now the server location and the crucial parts of the `server.xml` are also documented in the post, maybe there is still something wrong with them. – BairDev Apr 14 '21 at 19:20

1 Answers1

0

The most crucial changes have been

  1. Removing the Eclipse server from project references
  2. Set the execution environment to JavaSE-11 (see screenshot 1)
  3. Use the right deployment assembly (see screenshot 2), so mvn clean package and let the server run with the servlet project as source creates the same result. See also this SO answer.

screenshot 1

screenshot 2

BairDev
  • 2,865
  • 4
  • 27
  • 50