Java/Wicket: Compile Basic Hello World with Resources - java

I am following this example of a Hello World Wicket application
https://www.ibm.com/developerworks/web/library/wa-aj-wicket/
In particular I placed HelloWorld.html in my source directory next to HelloWorld.java.
My file structure looks like this:
$ tree
.
├── pom.xml
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │   └── example
│   │   │   └── wicket
│   │   │   ├── HelloWorld.html
│   │   │   ├── HelloWorld.java
│   │   │   └── HelloWorldApplication.java
│   │   ├── resources
│   │   └── webapp
│   │   └── WEB-INF
│   │   └── web.xml
│   └── test
│   └── java
└── wicketTest.iml
However when I compile this to a war file, and load in Jetty, i recieve this error, in the browser:
Unexpected RuntimeException
Last cause: Can not determine Markup. Component is not yet connected to a parent. [Page class = com.example.wicket.HelloWorld, id = 4, render count = 1]
Stacktrace
Root cause:
org.apache.wicket.markup.MarkupNotFoundException: Can not determine Markup. Component is not yet connected to a parent. [Page class = com.example.wicket.HelloWorld, id = 4, render count = 1]
at org.apache.wicket.Component.getMarkup(Component.java:737)
at org.apache.wicket.Component.internalRender(Component.java:2344)
at org.apache.wicket.Component.render(Component.java:2307)
at org.apache.wicket.Page.renderPage(Page.java:1010)
When I look in the war file I notice that the html file is missing:
$ tar tvf target/wicketTest-1.0-SNAPSHOT.war
drwxrwxrwx 0 0 0 0 Aug 22 14:50 META-INF/
-rwxrwxrwx 0 0 0 128 Aug 22 14:50 META-INF/MANIFEST.MF
drwxrwxrwx 0 0 0 0 Aug 22 14:50 WEB-INF/
drwxrwxrwx 0 0 0 0 Aug 22 14:50 WEB-INF/classes/
drwxrwxrwx 0 0 0 0 Aug 22 14:50 WEB-INF/classes/com/
drwxrwxrwx 0 0 0 0 Aug 22 14:50 WEB-INF/classes/com/example/
drwxrwxrwx 0 0 0 0 Aug 22 14:50 WEB-INF/classes/com/example/wicket/
drwxrwxrwx 0 0 0 0 Aug 22 14:50 WEB-INF/lib/
-rwxrwxrwx 0 0 0 608 Aug 22 14:50 WEB-INF/classes/com/example/wicket/HelloWorld.class
-rwxrwxrwx 0 0 0 551 Aug 22 14:50 WEB-INF/classes/com/example/wicket/HelloWorldApplication.class
-rwxrwxrwx 0 0 0 25962 Aug 21 16:07 WEB-INF/lib/slf4j-api-1.6.4.jar
-rwxrwxrwx 0 0 0 2126440 Aug 21 16:07 WEB-INF/lib/wicket-core-6.10.0.jar
-rwxrwxrwx 0 0 0 86671 Aug 21 16:07 WEB-INF/lib/wicket-request-6.10.0.jar
-rwxrwxrwx 0 0 0 415858 Aug 21 16:07 WEB-INF/lib/wicket-util-6.10.0.jar
-rwxrwxrwx 0 0 0 690 Aug 22 13:22 WEB-INF/web.xml
drwxrwxrwx 0 0 0 0 Aug 22 14:50 META-INF/maven/
drwxrwxrwx 0 0 0 0 Aug 22 14:50 META-INF/maven/wicketTest/
drwxrwxrwx 0 0 0 0 Aug 22 14:50 META-INF/maven/wicketTest/wicketTest/
-rwxrwxrwx 0 0 0 675 Aug 22 08:52 META-INF/maven/wicketTest/wicketTest/pom.xml
-rwxrwxrwx 0 0 0 112 Aug 22 14:50 META-INF/maven/wicketTest/wicketTest/pom.properties
How do I specify in my POM file to include the html file?
My POM right now is minimal:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>wicketTest</groupId>
<artifactId>wicketTest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-core</artifactId>
<version>6.10.0</version>
</dependency>
</dependencies>
</project>

The solution, if you want your HTML in the wicket best practice place (with your classes) is to add this to the build section of your pom.
<build>
<resources>
<resource>
<filtering>false</filtering>
<directory>src/main/resources</directory>
</resource>
<resource>
<filtering>false</filtering>
<directory>src/main/java</directory>
<includes>
<include>**</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
</build>
</project>

You should put you HelloWorld.html file into src/main/webapp folder. This way it will be included in the war file

If using Maven, see David Williams' answer. If using Gradle, see this answer.
Include the following in your build.gradle file:
sourceSets {
main {
resources {
srcDirs += ['src/main/java']
includes = ["**"]
// or specifically: includes = ["**/*.html"]
}
}
}
This ensures that the HTML files will be added to the WAR file.

Related

Unable to use submodule dependency class in test, throws NoClassDefFoundError

I am working on a maven multi-module project with the following structure
project/
├── migration/
│ ├── src/
│ │ ├── main/
│ │ │ └── Migration.java
│ │ └── test
│ └── pom.xml
├── depends-on-migration/
│ ├── src/
│ │ ├── main (uses Migration.java - ok)
│ │ └── test (uses Migration.java - throws NoClassDefFoundError)
│ └── pom.xml (depends on migration)
└── pom.xml
The class can be used (see image), but cannot compile when I run mvnw package (see logs)
2022-12-17T14:22:56.025+08:00 INFO 13468 --- [ main] c.b.d.DependsOnMigrationApplicationTests : Starting DependsOnMigrationApplicationTests using Java 17.0.2 with PID 13468 (started by Joseph in Z:\bwgjoseph\maven-nested-multi-module-project\depends-on-migration)
2022-12-17T14:22:56.031+08:00 INFO 13468 --- [ main] c.b.d.DependsOnMigrationApplicationTests : No active profile set, falling back to 1 default profile: "default"
2022-12-17T14:22:57.289+08:00 INFO 13468 --- [ main] c.b.d.DependsOnMigrationApplicationTests : Started DependsOnMigrationApplicationTests in 1.534 seconds (process running for 2.498)
[ERROR] Tests run: 2, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.493 s <<< FAILURE! - in com.bwgjoseph.dependsonmigration.DependsOnMigrationApplicationTests
[ERROR] test1 Time elapsed: 0.454 s <<< ERROR!
java.lang.NoClassDefFoundError: com/bwgjoseph/migration/Migration
at com.bwgjoseph.dependsonmigration.DependsOnMigrationApplicationTests.test1(DependsOnMigrationApplicationTests.java:17)
Caused by: java.lang.ClassNotFoundException: com.bwgjoseph.migration.Migration
at com.bwgjoseph.dependsonmigration.DependsOnMigrationApplicationTests.test1(DependsOnMigrationApplicationTests.java:17)
[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR] DependsOnMigrationApplicationTests.test1:17 NoClassDefFound com/bwgjoseph/migr...
[INFO]
[ERROR] Tests run: 2, Failures: 0, Errors: 1, Skipped: 0
This is a simplified example, can refer to repo (migration and depends-on-migration module)
I couldn't find anything similar, the closest I could find are cases where they are looking to use classes that are defined in migration/src/test but I am using migration/src/main
Glad to provide more information if required.
Thanks!
The problem stems from the fact that you use spring-boot-maven-plugin in both migration and depends-on-migration modules.
Spring Boot Maven Plugin produces an Uber-Jar, with a layout expected by Spring apps. The original jar (without dependencies) is renamed to migration-0.0.1-SNAPSHOT.jar.original
The structure of uber-jar
.
├── BOOT-INF
│ ├── classes
│ ├── classpath.idx
│ ├── layers.idx
│ └── lib
├── META-INF
├── migration-0.0.1-SNAPSHOT.zip
└── org
└── springframework
Your code is placed in BOOT-INF/classes - this is not a typical library layout, hence ClassNotFoundException.
See The Executable Jar Format for reference.
If you remove spring-boot-maven-plugin in migration project, mvn package passes.

Error: Unable to access jarfile in docker-compose

Below is my docker-compose.yml, which is pretty much simple.
version: "3.7"
services:
book-api:
image: openjdk:11
container_name: book-api
volumes:
- ./target/demo-0.0.1-SNAPSHOT.jar:/bookapi.jar
command: ["java","-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8080","-Dspring.profiles.active=local","-jar","bookapi.jar"]
ports:
- "8080:8080"
But when I do docker-compose up, I get the below error:
Creating book-api ... done
Attaching to book-api
book-api | Error: Unable to access jarfile bookapi.jar
book-api exited with code 1
I don't see an error with mounting volumes. jar file (target/demo-0.0.1-SNAPSHOT.jar) is created and is in the correct location.
I also tried to change command to below, thinking that was causing issue:
["java","-jar","bookapi.jar"]
But I'm getting the same error. I'm running docker-compose on mac.
To troubleshoot the issue, I changed the command to check the file paths, permissions etc.
command: >
sh -c "ls -l && pwd && java -jar bookapi.jar"
And then I see below results:
rahulraj#my-pc % docker-compose up
Starting book-api ... done
Attaching to book-api
book-api | total 46764
book-api | drwxr-xr-x 1 root root 4096 Jun 23 15:20 bin
book-api | -rwxrwxrwx 1 root root 47821378 Jun 27 15:25 bookapi.jar
book-api | drwxr-xr-x 2 root root 4096 Mar 19 13:46 boot
book-api | drwxr-xr-x 5 root root 340 Jun 28 06:50 dev
book-api | drwxr-xr-x 1 root root 4096 Jun 28 06:48 etc
book-api | drwxr-xr-x 2 root root 4096 Mar 19 13:46 home
book-api | drwxr-xr-x 1 root root 4096 Jun 22 00:00 lib
book-api | drwxr-xr-x 2 root root 4096 Jun 22 00:00 media
book-api | drwxr-xr-x 2 root root 4096 Jun 22 00:00 mnt
book-api | drwxr-xr-x 2 root root 4096 Jun 22 00:00 opt
book-api | dr-xr-xr-x 183 root root 0 Jun 28 06:50 proc
book-api | drwx------ 1 root root 4096 Jun 23 15:21 root
book-api | drwxr-xr-x 3 root root 4096 Jun 22 00:00 run
book-api | drwxr-xr-x 1 root root 4096 Jun 23 01:11 sbin
book-api | drwxr-xr-x 2 root root 4096 Jun 22 00:00 srv
book-api | dr-xr-xr-x 13 root root 0 Jun 28 06:50 sys
book-api | drwxrwxrwt 1 root root 4096 Jun 23 15:21 tmp
book-api | drwxr-xr-x 1 root root 4096 Jun 22 00:00 usr
book-api | drwxr-xr-x 1 root root 4096 Jun 22 00:00 var
book-api | /
book-api | Error: Unable to access jarfile bookapi.jar
book-api exited with code 1
Seems like the jar file is having full permissions, current working directory is / and path of jar file is /bookapi.jar. But the command java -jar /bookapi.jar is failing with the above mentioned error. I tried with java -jar bookapi.jar too.
Am I doing something wrong here? Kindly suggest. Thanks!
I'm not sure why it was not working while using only docker-compose.yml. So, the above mentioned issue is still a mystery unresolved, however I changed the docker config to below and no more issues observed.
Created a docker image of the application with a Dockerfile
From openjdk:11
copy ./target/app.jar app.jar
CMD ["java","-jar","app.jar"]
and then docker image build -t book-api .
Modified docker-compose.yml as below:
services:
book-api:
image: book-api
container_name: book-api
ports:
- "8080:8080"
and then docker-compose up worked without any issues and container is up and running.
Thank you #knittl for your inputs.

Cannot generate allure report from allure docker

I have tested with testng.xml and got the folder called allure-results on my linux host. This folder has many .json files
-rw-rw-r-- 1 root root 202 Jan 27 03:20 038526c0-e53c-4907-a04b-7061eff12e91-container.json
-rw-rw-r-- 1 root root 548 Jan 27 03:20 17bd74ad-1f5e-4ef9-9f21-b2ec85cfca47-container.json
-rw-rw-r-- 1 root root 419 Jan 27 03:20 1bd85195-ab0d-4e17-ab12-800e5037405f-container.json
-rw-rw-r-- 1 root root 195 Jan 27 03:20 1f6a2505-8c58-452d-a564-e7ffa9512599-container.json
-rw-rw-r-- 1 root root 837 Jan 27 03:20 4fb8431a-162a-4f33-889b-2e5ee5c01843-result.json
-rw-rw-r-- 1 root root 415 Jan 27 03:20 990031d8-60eb-4379-8864-aee237fed175-container.json
The docker-compose file I used to create a container is as below.
version: '2'
services:
allure:
image: "frankescobar/allure-docker-service"
environment:
CHECK_RESULTS_EVERY_SECONDS: 1
KEEP_HISTORY: 1
ports:
- "5050:5050"
volumes:
- $PROJECT_PATH/allure-results:/app/allure-results
- $PROJECT_PATH/allure-reports:/app/default-reports
The project name I created in allure-docker is test.
To generate allure report, I request this URL.
http://192.168.100.58:5050/allure-docker-service/generate-report?project_id=test but got nothing in there.
I can see the same .json files in the container's path as they are correctly mounted as a volume.
allure#0ff7067fedb0:/app/allure-results$ ll
total 36
drwxrwxr-x 2 root root 4096 Jan 27 03:20 ./
drwxr-xr-x 1 allure allure 4096 Dec 14 10:03 ../
-rw-rw-r-- 1 root root 202 Jan 27 03:20 038526c0-e53c-4907-a04b-7061eff12e91-container.json
-rw-rw-r-- 1 root root 548 Jan 27 03:20 17bd74ad-1f5e-4ef9-9f21-b2ec85cfca47-container.json
-rw-rw-r-- 1 root root 419 Jan 27 03:20 1bd85195-ab0d-4e17-ab12-800e5037405f-container.json
-rw-rw-r-- 1 root root 195 Jan 27 03:20 1f6a2505-8c58-452d-a564-e7ffa9512599-container.json
-rw-rw-r-- 1 root root 837 Jan 27 03:20 4fb8431a-162a-4f33-889b-2e5ee5c01843-result.json
-rw-rw-r-- 1 root root 415 Jan 27 03:20 990031d8-60eb-4379-8864-aee237fed175-container.json
allure#0ff7067fedb0:/app/allure-results$
Question What am I missing to get the allure report?
I found the answer myself.
To generate a report, actually, I don't have to generate it manually as this allure-docker generates reports automatically if new results in the results directory are detected.
To get the report, remove allure-docker-service from URL request. Hence, it should be like,
http://192.168.100.58:5050/generate-report?project_id=test

COPY failed error dockerfile while using gcloud command

Some strange error has hit, this code was working earlier.
My tree:
LM0002254:SellerService$ ls -lrt
total 61512
drwxr-xr-x 3 tarun 826136866 96 Feb 8 14:16 gradle
-rw-r--r-- 1 tarun 826136866 168 Apr 23 13:26 gradle.properties
-rwxr-xr-x 1 tarun 826136866 5766 Apr 23 13:26 gradlew
-rw-r--r-- 1 tarun 826136866 2763 Apr 23 13:26 gradlew.bat
-rw-r--r-- 1 tarun 826136866 36 Apr 23 13:26 settings.gradle.kts
drwxr-xr-x 5 tarun 826136866 160 Apr 23 13:26 src
-rw-r--r-- 1 tarun 826136866 3868 Apr 23 22:39 build.gradle.kts
drwxr-xr-x 23 tarun 826136866 736 Apr 23 23:15 build
-rw-r--r-- 1 tarun 826136866 31282686 Apr 23 23:16 SellerService-jvm-1.0-SNAPSHOT.jar
-rwxr-xr-x 1 tarun 826136866 214 Apr 23 23:22 install-cloud.sh
-rw-r--r-- 1 tarun 826136866 148 Apr 23 23:39 Dockerfile
Docker file:
FROM openjdk:8-jre-alpine
WORKDIR /app
COPY SellerService-jvm-1.0-SNAPSHOT.jar ./
CMD ["java", "-jar", "./SellerService-jvm-1.0-SNAPSHOT.jar"]
Am getting below error, not sure why. This same piece of code was running yesterday.
cmd which am using on gcloud.
gcloud builds submit --tag gcr.io/<projectname>
Error am getting:
Step 1/5 : FROM openjdk:8-jre-alpine
8-jre-alpine: Pulling from library/openjdk
e7c96db7181b: Already exists
f910a506b6cb: Already exists
b6abafe80f63: Pulling fs layer
b6abafe80f63: Verifying Checksum
b6abafe80f63: Download complete
b6abafe80f63: Pull complete
Digest: sha256:f362b165b870ef129cbe730f29065ff37399c0aa8bcab3e44b51c302938c9193
Status: Downloaded newer image for openjdk:8-jre-alpine
---> f7a292bbb70c
Step 2/5 : WORKDIR /app
---> Running in 76b0acf0c246
Removing intermediate container 76b0acf0c246
---> 4afe2649b879
Step 3/5 : COPY SellerService-jvm-1.0-SNAPSHOT.jar .
COPY failed: stat /var/lib/docker/tmp/docker-builder987483752/SellerService-jvm-1.0-SNAPSHOT.jar: no such file or directory
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 1
Docker is reading my .gitignore content and ignoring files and folder in it. I have ignored jar files in gitignore so its ignoring that while copying. Need to make sure .gitignore file is not having those files ignored which you are copying in docker file.

simplest possible POJO echo with netty

How do I initiate an infinite loop between the client and server, to send and receive POJO PingPong objects between the client and server?
When a connection is established, which seems to be happening, the client should send a PingPong, and the server should reply with a PingPong POJO as well. The client should (preferably) respond with another PingPong, creating an infinite loop.
the server receives an object:
BUILD SUCCESSFUL
Total time: 2 seconds
Jul 20, 2014 2:43:17 PM io.netty.handler.logging.LoggingHandler channelRegistered
INFO: [id: 0x2cd55a8d] REGISTERED
Jul 20, 2014 2:43:17 PM io.netty.handler.logging.LoggingHandler bind
INFO: [id: 0x2cd55a8d] BIND(0.0.0.0/0.0.0.0:4454)
Jul 20, 2014 2:43:17 PM io.netty.handler.logging.LoggingHandler channelActive
INFO: [id: 0x2cd55a8d, /0:0:0:0:0:0:0:0:4454] ACTIVE
Jul 20, 2014 2:43:24 PM io.netty.handler.logging.LoggingHandler logMessage
INFO: [id: 0x2cd55a8d, /0:0:0:0:0:0:0:0:4454] RECEIVED: [id: 0xe7c06459, /127.0.0.1:33182 => /127.0.0.1:4454]
^Cthufir#dur:~/NetBeansProjects/NettyServer$
thufir#dur:~/NetBeansProjects/NettyServer$
which was sent by the client:
BUILD SUCCESSFUL
Total time: 2 seconds
Jul 20, 2014 2:43:24 PM io.netty.handler.logging.LoggingHandler channelRegistered
INFO: [id: 0x0f1fc251] REGISTERED
Jul 20, 2014 2:43:24 PM io.netty.handler.logging.LoggingHandler connect
INFO: [id: 0x0f1fc251] CONNECT(localhost/127.0.0.1:4454, null)
Jul 20, 2014 2:43:24 PM io.netty.handler.logging.LoggingHandler channelActive
INFO: [id: 0x0f1fc251, /127.0.0.1:33182 => localhost/127.0.0.1:4454] ACTIVE
^Cthufir#dur:~/NetBeansProjects/NettyClient$
thufir#dur:~/NetBeansProjects/NettyClient$
server code:
package net.bounceme.dur.netty;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.net.Socket;
import java.util.logging.Logger;
#Sharable
public class PingPongServerHandler extends SimpleChannelInboundHandler<PingPong> {
private static final Logger log = Logger.getLogger(PingPongServerHandler.class.getName());
protected Socket socket = null;
#Override
protected void channelRead0(ChannelHandlerContext chc, PingPong pingPong) throws Exception {
log.info(pingPong.getClass().getSimpleName());
chc.writeAndFlush(new PingPong());
}
}
client code:
package net.bounceme.dur.netty;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.util.logging.Logger;
public class PingPongClientHandler extends SimpleChannelInboundHandler<PingPong> {
private static final Logger log = Logger.getLogger(PingPongClientHandler.class.getName());
public PingPongClientHandler() {
}
#Override
protected void channelRead0(ChannelHandlerContext chc, PingPong pingPong) throws Exception {
log.info("reading...");
chc.writeAndFlush(new PingPong());
}
}
adapted from:
https://github.com/netty/netty/tree/4.0/example/src/main/java/io/netty/example/objectecho
branch 4.0
see also:
https://community.jboss.org/wiki/NettyExampleOfPingPongUsingObject
which uses 3.x and not 4.x, and is overly complex for my purposes.
server:
src/
├── net
│   └── bounceme
│   └── dur
│   └── netty
│   ├── EchoServer.java
│   ├── MyProps.java
│   ├── PingPong.java
│   └── PingPongServerHandler.java
└── server.properties
client:
src
├── net
│   └── bounceme
│   └── dur
│   └── netty
│   ├── EchoClient.java
│   ├── MyProps.java
│   ├── PingPongClientHandler.java
│   └── PingPong.java
└── server.properties
I'm trying to adapt the Echo Netty example to use POJO's instead of ByteBuf.

Categories