Closed thilinamb closed 8 years ago
The problem is about modules in a maven project. By following this stackoverflow threadI have it worked out.
Firstly create a directory, say netty-example
, where source files in each chapter will reside.
mkdir netty-example
cd netty-example
then
mvn archetype:generate \
-DarchetypeGroupId=org.codehaus.mojo.archetypes \
-DarchetypeArtifactId=pom-root \
-DarchetypeVersion=RELEASE
while prompt asks for groupId
and artifactId
, give such input:
this creates a maven project for chapter 2, the structure of directory netty-example
would looks like this:
- netty-example
- chapter02
- pom.xml
Then, go into directory chapter02
, and creates two maven module for server and client side respectively:
mvn archetype:generate \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=RELEASE
While asks for groupId
and artifactId
, for both server
and client
modules, the groupId
would be com.example
, and the artifactId
would be server
and client
respectively.
Copy EchoClient.java
and EchoClientHandler
into client/src/main/java/com/example/
, then copy EchoServer.java
and EchoServerHandler
into server/src/main/java/com/example
. Right now, the structure of directory netty-example
should looks like this(remember to delete App.class
pre generated in both directory):
Change pom.xml
inside directory chapter02
with following content:
<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>com.chengevo</groupId>
<artifactId>netty-demon</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>server</module>
<module>client</module>
</modules>
<packaging>pom</packaging>
<name>netty-demon</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<optimize>true</optimize>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${netty.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.npn</groupId>
<artifactId>npn-api</artifactId>
<version>${npn.version}</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty.npn</groupId>
<artifactId>npn-boot</artifactId>
<version>${npn.boot.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<properties>
<npn.version>1.1.0.v20120525</npn.version>
<npn.boot.version>1.1.10.v20150130</npn.boot.version>
<netty.version>4.0.25.Final</netty.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
</project>
Change the pom.xml
inside client
directory into this:
<?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">
<parent>
<artifactId>chapter02</artifactId>
<groupId>com.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>client</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.EchoClient</mainClass>
<arguments>
<argument>0.0.0.0</argument>
<argument>9999</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
Change the pom.xml
inside server
directory into this:
<?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">
<parent>
<artifactId>chapter02</artifactId>
<groupId>com.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>server</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.EchoServer</mainClass>
<arguments>
<argument>9999</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
Note:
mainClass
tag specifies which class will be executed while the program runsargument
tag nested in arguments
tag specifies the first, the second etc command line argument passed to the program. Thus in the example above, the first command line argument is 0.0.0.0
, and the second command line argument is 9999
, ie the port that client and server listens to.the go back to directory chapter02
, run the command
mvn clean package
inside client
and server
directory, fires up both client and server side:
mvn exec:java
The latest sample code is https://github.com/normanmaurer/netty-in-action/tree/2.0-SNAPSHOT
The latest sample code is
https://github.com/normanmaurer/netty-in-action/tree/2.0-SNAPSHOT
Marvin Wolfthal
email: maw@weichi.com
From: James Tweeddale [mailto:notifications@github.com] Sent: Sunday, January 31, 2016 6:12 PM To: normanmaurer/netty-in-action Subject: Re: [netty-in-action] Inconsistencies in the code structure between the book and the Github repo (#20)
I am having the same issue as well. It is disappointing that the instructions the book provides for building the examples are so glaringly incorrect. To have to perform the above procedure for every chapter's examples is frustrating.
— Reply to this email directly or view it on GitHub https://github.com/normanmaurer/netty-in-action/issues/20#issuecomment-177655526 . https://github.com/notifications/beacon/ADCkv_n5eqhx25yC2bzAc6c2u1Derr8Vks5pfoxQgaJpZM4FOXOg.gif
Thanks!
As per the book, there should be a separate Maven sub module for each chapter. But it is a flat structure in the Github. Due to this inconsistency, the instructions for running some of the examples (e.g. ch.2) are invalid.