Home > Software engineering >  How to add Jetty-Servlets into Java Project via Maven for Cross-Origin-Header
How to add Jetty-Servlets into Java Project via Maven for Cross-Origin-Header

Time:01-16

I am working with the PESTO project from this repository. To use this project for my purpose I have to add Cross-Origin-Headers, such that I am able to call this identity provider via JavaScript. The following code adds the Cross-Origin-Header to the RestIdPServer.java file:

FilterHolder filterHolder = context.addFilter(
                org.eclipse.jetty.servlets.CrossOriginFilter.class,
                "/*",
                EnumSet.of(DispatcherType.REQUEST));
        filterHolder.setInitParameter("allowedOrigins", "*");

The server uses an embedded Jetty Server. After doing mvn initialize and mvn clean install package -DskipTests I try to run the artifact in target the so-called PESTO-IdP-jar-with-dependencies.jar.

The problem is that I don't get it to work to include the CrossOriginFilter class. This is the error output via the command java -jar target/PESTO-IdP-jar-with-dependencies.jar 9080 0 9998 keystore.jks server1 server1:

STARTING REST IdP SERVER :9080-eu.olympus.server.PestoIdP@816f27d
2022-01-10 07:49:52.618:INFO::main: Logging initialized @159ms to org.eclipse.jetty.util.log.StdErrLog
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/jetty/servlets/CrossOriginFilter
    at eu.olympus.server.rest.RESTIdPServer.start(RESTIdPServer.java:89)
    at eu.olympus.server.RunPestoServer.main(RunPestoServer.java:49)
Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.servlets.CrossOriginFilter
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 2 more

I tried also different configurations inside of the pom.xml. This is the actual pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>eu.olympus</groupId>
  <artifactId>core</artifactId>
  <name>core</name>
  <version>0.0.1</version>
  <packaging>jar</packaging>
  <licenses>
    <license>
      <name>Apache License Version 2.0</name>
      <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
      <distribution>repo</distribution>
    </license>
  </licenses>
  <build>
    <finalName>PESTO-IdP</finalName>
    <plugins>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.5</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-release-plugin</artifactId>
        <inherited>true</inherited>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.2.0</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>

          </descriptorRefs>
          <archive>
            <manifestEntries>
            <Class-Path>libs/amcl-3.2-SNAPSHOT.jar</Class-Path>
<!--              <Class-Path>libs/jetty-servlets-9.4.3.v20170317.jar</Class-Path>-->
            </manifestEntries>
            <manifest>
              <addClasspath>true</addClasspath>
                            <mainClass>eu.olympus.server.RunPestoServer</mainClass>
              <!--              <mainClass>eu.olympus.server.RunPasswordJWTServer</mainClass> -->
              <classpathPrefix>libs/</classpathPrefix>
            </manifest>

          </archive>

        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <!-- Comment in to construct benchmarking jar -->
<!--      <plugin>-->
<!--        <artifactId>maven-assembly-plugin</artifactId>-->
<!--        <version>2.3</version>-->
<!--        <configuration>-->
<!--          <descriptor>src/main/assembly/assembly.xml</descriptor>-->
<!--        </configuration>-->
<!--        <executions>-->
<!--          <execution>-->
<!--            <id>make-assembly</id>-->
<!--            <phase>package</phase>-->
<!--            <goals>-->
<!--              <goal>single</goal>-->
<!--            </goals>-->
<!--            <configuration>-->
<!--              <archive>-->
<!--                <manifest>-->
<!--                  <mainClass>eu.olympus.benchmark.Benchmark</mainClass>-->
<!--                </manifest>-->
<!--              </archive>-->
<!--            </configuration>-->
<!--          </execution>-->
<!--        </executions>-->
<!--      </plugin>-->

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.5.1</version>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/libs/</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>2.8</version>
      </plugin>

      <!-- PLUGIN FOR INSTALLING amcl -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>install1</id>
            <phase>initialize</phase>
            <goals>
              <goal>install-file</goal>
            </goals>
            <configuration>

              <groupId>org.apache.milagro</groupId>
              <artifactId>amcl</artifactId>
              <version>3.2</version>
              <packaging>jar</packaging>
              <file>${project.basedir}/libs/amcl-3.2-SNAPSHOT.jar</file>
            </configuration>
          </execution>
<!--          <execution>-->
<!--            <id>install2</id>-->
<!--            <phase>initialize</phase>-->
<!--            <goals>-->
<!--              <goal>install-file</goal>-->
<!--            </goals>-->
<!--            <configuration>-->

<!--              <groupId>org.eclipse.jetty</groupId>-->
<!--              <artifactId>jetty-servlets</artifactId>-->
<!--              <version>9.4.3.v20170317</version>-->
<!--              <packaging>jar</packaging>-->
<!--              <file>${project.basedir}/libs/jetty-servlets-9.4.3.v20170317.jar</file>-->
<!--            </configuration>-->
<!--          </execution>-->
        </executions>
      </plugin>

      <!-- CHECKSTYLE -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <version>2.17</version>
        <configuration>
          <linkXRef>false</linkXRef>
          <configLocation>google_checks.xml</configLocation>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>com.puppycrawl.tools</groupId>
            <artifactId>checkstyle</artifactId>
            <version>7.6</version>
          </dependency>
        </dependencies>
      </plugin>

      <!-- CODE COVERAGE -->
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.0</version>
        <executions>
          <execution>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
          <execution>
            <id>jacoco-check</id>
            <phase>test</phase>
            <goals>
              <goal>report</goal>
              <goal>check</goal>
            </goals>
            <configuration>
              <rules>
                <rule>
                  <element>PACKAGE</element>
                  <!--          <limits>
                            <limit>
                        <counter>BRANCH</counter>
                        <value>COVEREDRATIO</value>
                        <minimum>1</minimum>
                            </limit>

                            <limit>
                        <counter>LINE</counter>
                        <value>COVEREDRATIO</value>
                        <minimum>0.95</minimum>
                            </limit>
                          </limits>-->
                </rule>
              </rules>
            </configuration>
          </execution>
          <!--      <execution>
                <id>report</id>
                <phase>test</phase>
                <goals>
                  <goal>report</goal>
                </goals>
                </execution> -->
        </executions>
      </plugin>
    </plugins>
  </build>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

  <dependencies>
    <!-- JUnit -->
<!--    <dependency>-->
<!--      <groupId>junit</groupId>-->
<!--      <artifactId>junit</artifactId>-->
<!--      <version>4.11</version>-->
<!--      <scope>test</scope>-->
<!--    </dependency>-->

    <!-- Logging -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-web</artifactId>
      <version>2.0</version>
    </dependency>

    <!-- WEBSERVICES START -->
    <!-- JAX-RS -->
    <dependency>
      <groupId>javax.ws.rs</groupId>
      <artifactId>javax.ws.rs-api</artifactId>
      <version>2.0</version>
    </dependency>

    <!-- Jetty -->
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-server</artifactId>
      <version>9.4.3.v20170317</version>
    </dependency>

    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-servlet</artifactId>
      <version>9.4.3.v20170317</version>
      <!--  <version>9.4.22.v20191022</version> -->
    </dependency>

    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-servlets</artifactId>
      <scope>system</scope>
      <systemPath>${project.basedir}/libs/jetty-servlets-9.4.3.v20170317.jar</systemPath>
      <version>9.4.3.v20170317</version>
    </dependency>

    <!-- Jersey -->
    <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-json-jackson</artifactId>
      <version>2.25.1</version>
    </dependency>

    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet</artifactId>
      <version>2.25.1</version>

    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.core</groupId>
      <artifactId>jersey-client</artifactId>
      <version>2.25.1</version>
    </dependency>

    <!-- Http client -->
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.9</version>
    </dependency>

    <!-- JWT (openID Connect) IdP -->
    <dependency>
      <groupId>com.auth0</groupId>
      <artifactId>java-jwt</artifactId>
      <version>3.8.1</version>
    </dependency>

    <dependency>
      <groupId>com.googlecode.json-simple</groupId>
      <artifactId>json-simple</artifactId>
      <version>1.1.1</version>
    </dependency>


    <!-- Bilinear pairings implementation -->
    <dependency>
      <groupId>org.apache.milagro</groupId>
      <artifactId>amcl</artifactId>
      <version>3.2-SNAPSHOT</version>
      <scope>system</scope>
      <systemPath>${project.basedir}/libs/amcl-3.2-SNAPSHOT.jar</systemPath>
    </dependency>

    <!-- Jackson -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.8.9</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.8.9</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.8.9</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.jaxrs</groupId>
      <artifactId>jackson-jaxrs-json-provider</artifactId>
      <version>2.8.9</version>
    </dependency>
    <dependency>
      <groupId>org.hamcrest</groupId>
      <artifactId>hamcrest-core</artifactId>
      <version>1.3</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>compile</scope>
    </dependency>
    <!-- WEBSERVICES END -->
    <dependency>
      <groupId>com.google.protobuf</groupId>
      <artifactId>protobuf-java</artifactId>
      <version>3.10.0</version>
    </dependency>
<!--    <dependency>-->
<!--      <groupId>eu.olympus</groupId>-->
<!--      <artifactId>core</artifactId>-->
<!--      <version>0.0.1</version>-->
<!--    </dependency>-->
  </dependencies>

</project>

Additionally, I added the jetty-servlets-9.4.3.v20170317.jar to libs directory that is mentioned inside of the pom.xml file.

I searched a while for a solution for it, but wasn't able to solve it on my own. Do I miss something in the pom.xml that the missing class is not included inside of the resulting artifact PESTO-IdP-jar-with-dependencies.jar or how I am able to include the library into the Jar artifact?

CodePudding user response:

I found a solution for my problem. The following steps solved my issue.

  1. Add the dependency without <scope>system</scope> and <systemPath>...</systemPath> In my case I had to add:
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-servlets</artifactId>
      <version>9.4.3.v20170317</version>
    </dependency>
  1. Add the whole classpath that is necessary for the build. The following part includes the <goal>build-classpath</goal> inside of the maven-dependecy-plugin:
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.5.1</version>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
              <goal>copy-dependencies</goal>
              <goal>build-classpath</goal> <!-- new -->
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/libs/</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
  1. Add the installation of this external library in the initialize phase. The following code do that for me:
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>install-file</goal>
            </goals>
            <configuration>

              <groupId>org.eclipse.jetty</groupId>
              <artifactId>jetty-servlets</artifactId>
              <version>9.4.3.v20170317</version>
              <packaging>jar</packaging>
              <file>${project.basedir}/libs/jetty-servlets-9.4.3.v20170317.jar</file>
            </configuration>
          </execution>
        </executions>
      </plugin>

These three steps solved my issue. I hope that helps other people, who face similar problems.

  •  Tags:  
  • Related