Commits

Zemian Deng  committed 744b2ff

Added maven-multi proj.

  • Participants
  • Parent commits 621d9dc

Comments (0)

Files changed (16)

File maven-multi-projects/README.md

+# Description
+
+A Spring based multi-modules Maven project.

File maven-multi-projects/mm-data/pom.xml

+<?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>
+	<artifactId>mm-data</artifactId>
+	<packaging>jar</packaging>
+
+	<parent>
+		<groupId>multi-modules-project</groupId>
+		<artifactId>mm-parent</artifactId>
+		<version>1.0.0-SNAPSHOT</version>
+		<relativePath>../pom.xml</relativePath>
+	</parent>
+
+</project>

File maven-multi-projects/mm-data/src/main/java/deng/HelloService.java

+package deng;
+import org.slf4j.*;
+import java.util.concurrent.atomic.*;
+
+/**
+ * A simple service class.
+ * @author Zemian Deng <saltnlight5@gmail.com>
+ */
+public class HelloService {
+	private static Logger logger = LoggerFactory.getLogger(HelloService.class);
+	private AtomicBoolean started = new AtomicBoolean(false);
+	public boolean isStarted() { return started.get(); }
+	public void start() { started.set(true); logger.info("started."); }
+	public void stop() { started.set(false); logger.info("stopped."); }
+}

File maven-multi-projects/mm-data/src/main/java/deng/SpringRunner.java

+package deng;
+import org.slf4j.*;
+import org.springframework.context.support.*;
+
+/**
+ * A small Spring server that start up a Spring context and put main thread to wait mode until user hit CTRL+C.
+ * @author Zemian Deng <saltnlight5@gmail.com>
+ */
+public class SpringRunner {
+	public static void main(String[] args) throws Exception {
+		SpringRunner runner = new SpringRunner();
+		runner.run(args);
+	}
+
+	private static Logger logger = LoggerFactory.getLogger(SpringRunner.class);
+	private boolean wait = true;
+
+	public void setWait(boolean flag) {
+		this.wait = flag;
+	}
+
+	public void run(String[] args) throws Exception {
+		// Get wait option
+		String waitFlag = System.getProperty("wait");
+		if (waitFlag != null)
+			wait = Boolean.parseBoolean(waitFlag);
+
+		// Start spring context.
+		logger.debug("Starting Spring context with " + java.util.Arrays.asList(args));
+		final FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext(args);
+		logger.info("Spring context started. context=" + ctx.toString());
+
+		// Allow spring context proper shutdown.
+		Runtime.getRuntime().addShutdownHook(new Thread() {
+			@Override
+			public void run() {
+				String ctxStr = ctx.toString();
+				logger.debug("Stopping Spring context " + ctxStr);
+				ctx.close();
+				logger.info("Spring context has stopped. context=" + ctxStr);
+			}
+		});
+
+		// Let this runner run as a server.
+		if (wait) {
+			synchronized(this) {
+				this.wait();
+			}
+		}
+	}
+}

File maven-multi-projects/mm-data/src/test/java/deng/HelloServiceTest.java

+package deng;
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+/**
+ * An integration test for HelloService.
+ * @author Zemian Deng <saltnlight5@gmail.com>
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration
+public class HelloServiceTest {
+
+	@Autowired
+	private HelloService helloService;
+
+	@Test
+	public void testHelloService() {
+		Assert.assertThat(helloService, Matchers.notNullValue());
+	}
+}

File maven-multi-projects/mm-data/src/test/resources/deng/HelloServiceTest-context.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<beans
+	xmlns="http://www.springframework.org/schema/beans"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
+
+	<bean id="helloService" class="deng.HelloService" init-method="start" destroy-method="stop">
+	</bean>
+
+</beans>

File maven-multi-projects/mm-data/src/test/resources/log4j.properties

+#
+# A log4j configuration file
+#
+
+# Basic STDOUT logger appender.
+log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
+log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
+log4j.appender.CONSOLE.layout.conversionPattern=%d{HH:mm:ss} %t %-5p %c:%L | %m%n
+
+# A rolling file logger appender.
+#log4j.appender.FILE=org.apache.log4j.RollingFileAppender
+#log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
+#log4j.appender.FILE.layout.conversionPattern=%d %-5p %t %c| %m%n
+#log4j.appender.FILE.threshold=DEBUG
+#log4j.appender.FILE.append=true
+#log4j.appender.FILE.file=application.log
+
+# Root logger level
+log4j.rootLogger = INFO, CONSOLE
+
+# Application logger level
+log4j.logger.deng = DEBUG

File maven-multi-projects/mm-dist/README.md

+# You may run our application like this
+
+bash> bin/run-spring config/hello-spring.xml

File maven-multi-projects/mm-dist/bin/run-java

+#!/usr/bin/env bash
+#
+# Copyright 2012 Zemian Deng
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# A wrapper script that run any Java application in unix/cygwin bash env.
+#
+# This script is assumed to be located in an application's "bin" directory. It will
+# auto resolve its directory location relative to the application path (which is one
+# parent up from the script.) Therefore, this script can be run any where in the file
+# system and it will still reference the same application directory.
+#
+# This script will by default auto setup a Java classpath that picks up any "config"
+# and "lib" directories under the application directory. It also will also add a
+# any typical Maven project output directories such as "target/test-classes",
+# "target/classes", and "target/dependency" into classpath. This can be disable by
+# setting RUN_JAVA_NO_AUTOCP=1.
+#
+# If the "Default parameters" section bellow doesn't match to user's env, then user
+# may override these variables in their terminal session or preset them in shell's
+# profile startup script. The values of all path should be in cygwin/unix path,
+# and this script will auto convert them into Windows path where is needed.
+#
+# User may customize the Java classpath by setting RUN_JAVA_CP, which will prefix to existing
+# classpath, or use the "-cp" option, which will postfix to existing classpath.
+#
+# Usage:
+#   run-java [java_opts] <java_main_class> [-cp /more/classpath] [-Dsysprop=value]
+#
+# Example:
+#   run-java example.Hello
+#   run-java example.Hello -Dname=World
+#   run-java org.junit.runner.JUnitCore example.HelloTest -cp "$HOME/apps/junit/lib/*"
+#
+# Created by: Zemian Deng 03/09/2012
+
+# This run script dir (resolve to absolute path)
+SCRIPT_DIR=$(cd $(dirname $0) && pwd)    # This dir is where this script live.
+APP_DIR=$(cd $SCRIPT_DIR/.. && pwd)      # Assume the application dir is one level up from script dir.
+
+# Default parameters
+JAVA_HOME=${JAVA_HOME:=$HOME/apps/jdk}     # This is the home directory of Java development kit.
+RUN_JAVA_CP=${RUN_JAVA_CP:=$CLASSPATH}     # A classpath prefix before -classpath option, default to $CLASSPATH
+RUN_JAVA_OPTS=${RUN_JAVA_OPTS:=}           # Java options (-Xmx512m -XX:MaxPermSize=128m etc)
+RUN_JAVA_DEBUG=${RUN_JAVA_DEBUG:=}         # If not empty, print the full java command line before executing it.
+RUN_JAVA_NO_PARSE=${RUN_JAVA_NO_PARSE:=}   # If not empty, skip the auto parsing of -D and -cp options from script arguments.
+RUN_JAVA_NO_AUTOCP=${RUN_JAVA_NO_AUTOCP:=} # If not empty, do not auto setup Java classpath
+RUN_JAVA_DRY=${RUN_JAVA_DRY:=}             # If not empty, do not exec Java command, but just print
+
+# OS specific support.  $var _must_ be set to either true or false.
+CYGWIN=false;
+case "`uname`" in
+  CYGWIN*) CYGWIN=true ;;
+esac
+
+# Define where is the java executable is
+JAVA_CMD=java
+if [ -d "$JAVA_HOME" ]; then
+	JAVA_CMD="$JAVA_HOME/bin/java"
+fi
+
+# Auto setup applciation's Java Classpath (only if they exists)
+if [ -z "$RUN_JAVA_NO_AUTOCP" ]; then
+	if [ -d "$APP_DIR/config" ]; then RUN_JAVA_CP="$RUN_JAVA_CP:$APP_DIR/config" ; fi
+	if [ -d "$APP_DIR/target/test-classes" ]; then RUN_JAVA_CP="$RUN_JAVA_CP:$APP_DIR/target/test-classes" ; fi
+	if [ -d "$APP_DIR/target/classes" ]; then RUN_JAVA_CP="$RUN_JAVA_CP:$APP_DIR/target/classes" ; fi
+	if [ -d "$APP_DIR/target/dependency" ]; then RUN_JAVA_CP="$RUN_JAVA_CP:$APP_DIR/target/dependency/*" ; fi
+	if [ -d "$APP_DIR/lib" ]; then RUN_JAVA_CP="$RUN_JAVA_CP:$APP_DIR/lib/*" ; fi
+fi
+
+ARGS="$@"
+# Parse addition "-cp" and "-D" after the Java main class from script arguments
+#   This is done for convenient sake so users do not have to export RUN_JAVA_CP and RUN_JAVA_OPTS
+#   saparately, but now they can pass into end of this run-java script instead.
+#   This can be disable by setting RUN_JAVA_NO_PARSE=1.
+if [ -z "$RUN_JAVA_NO_PARSE" ]; then
+	# Prepare variables for parsing
+	FOUND_CP=
+	NEW_ARGS[0]=''
+	IDX=0
+
+	# Parse all arguments and look for "-cp" and "-D"
+	for ARG in "$@"; do
+		if [[ -n $FOUND_CP ]]; then
+			RUN_JAVA_CP="$RUN_JAVA_CP:$ARG"
+			FOUND_CP=
+		else
+			case $ARG in
+			'-cp')
+				FOUND_CP=1
+				;;
+			'-D'*)
+				RUN_JAVA_OPTS="$RUN_JAVA_OPTS $ARG"
+				;;
+			*)
+				NEW_ARGS[$IDX]="$ARG"
+				let IDX=$IDX+1
+				;;
+			esac
+		fi
+	done
+	ARGS="${NEW_ARGS[@]}"
+fi
+
+# Convert Windows Java Classpath
+if $CYGWIN; then
+	RUN_JAVA_CP=$(cygpath -mp $RUN_JAVA_CP)
+fi
+
+# Display full Java command.
+if [ -n "$RUN_JAVA_DEBUG" ] || [ -n "$RUN_JAVA_DRY" ]; then
+	echo "$JAVA_CMD" $RUN_JAVA_OPTS -cp "$RUN_JAVA_CP" $ARGS
+fi
+
+# Run Java Main class
+if [ -z "$RUN_JAVA_DRY" ]; then
+	"$JAVA_CMD" $RUN_JAVA_OPTS -cp "$RUN_JAVA_CP" $ARGS
+fi

File maven-multi-projects/mm-dist/bin/run-spring

+#!/usr/bin/env bash
+DIR=$(cd `dirname $0` && pwd)
+$DIR/run-java deng.SpringRunner "$@"

File maven-multi-projects/mm-dist/config/hello-spring.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<beans
+	xmlns="http://www.springframework.org/schema/beans"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
+
+	<bean id="helloService" class="deng.HelloService" init-method="start" destroy-method="stop">
+	</bean>
+
+</beans>

File maven-multi-projects/mm-dist/config/log4j.properties

+#
+# A log4j configuration file
+#
+
+# Basic STDOUT logger appender.
+log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
+log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
+log4j.appender.CONSOLE.layout.conversionPattern=%d{HH:mm:ss} %t %-5p %c:%L | %m%n
+
+# A rolling file logger appender.
+#log4j.appender.FILE=org.apache.log4j.RollingFileAppender
+#log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
+#log4j.appender.FILE.layout.conversionPattern=%d %-5p %t %c| %m%n
+#log4j.appender.FILE.threshold=DEBUG
+#log4j.appender.FILE.append=true
+#log4j.appender.FILE.file=application.log
+
+# Root logger level
+log4j.rootLogger = INFO, CONSOLE
+
+# Application logger level
+#log4j.logger.org.springframework = DEBUG

File maven-multi-projects/mm-dist/pom.xml

+<?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>
+	<artifactId>mm-dist</artifactId>
+	<packaging>pom</packaging>
+
+	<parent>
+		<groupId>multi-modules-project</groupId>
+		<artifactId>mm-parent</artifactId>
+		<version>1.0.0-SNAPSHOT</version>
+		<relativePath>../pom.xml</relativePath>
+	</parent>
+
+	<build>
+		<plugins>
+			<plugin>
+				<artifactId>maven-deploy-plugin</artifactId>
+				<version>2.7</version>
+				<configuration>
+					<skip>true</skip>
+				</configuration>
+			</plugin>
+			<plugin>
+				<artifactId>maven-install-plugin</artifactId>
+				<version>2.4</version>
+				<configuration>
+					<skip>true</skip>
+				</configuration>
+			</plugin>
+			<plugin>
+				<artifactId>maven-assembly-plugin</artifactId>
+				<version>2.3</version>
+				<executions>
+					<execution>
+						<id>bin</id>
+						<phase>package</phase>
+						<goals>
+							<goal>single</goal>
+						</goals>
+						<configuration>
+							<appendAssemblyId>false</appendAssemblyId>
+							<finalName>${project.groupId}-${project.version}</finalName>
+							<descriptors>
+								<descriptor>src/main/assembly/bin.xml</descriptor>
+							</descriptors>
+						</configuration>
+					</execution>
+				</executions>
+			</plugin>
+		</plugins>
+	</build>
+
+</project>

File maven-multi-projects/mm-dist/src/main/assembly/bin.xml

+<?xml version="1.0"	encoding="UTF-8"?>
+<assembly 
+	xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  	xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
+	
+	<id>bin</id>
+	<formats>
+		<format>zip</format>
+	</formats>
+	<includeBaseDirectory>true</includeBaseDirectory>
+	<moduleSets>
+		<moduleSet>
+			<useAllReactorProjects>true</useAllReactorProjects>
+			<includes>
+				<include>*:*</include>
+			</includes>
+			<sources>
+				<fileSets>
+					<fileSet>
+						<includes>
+							<include>config/*</include>
+						</includes>
+					</fileSet>
+					<fileSet>
+						<includes>
+							<include>bin/*</include>
+						</includes>
+						<fileMode>775</fileMode>
+					</fileSet>
+					<fileSet>
+						<includes>
+							<include>README.*</include>
+						</includes>
+					</fileSet>
+				</fileSets>
+				<includeModuleDirectory>false</includeModuleDirectory>
+			</sources>
+			<binaries>
+				<outputDirectory>lib</outputDirectory>
+				<unpack>false</unpack>
+				<dependencySets>
+					<dependencySet>
+						<includes>
+							<include>*:jar</include>
+						</includes>
+						<unpack>false</unpack>
+						<scope>runtime</scope>
+					</dependencySet>
+				</dependencySets>
+			</binaries>
+		</moduleSet>
+	</moduleSets>
+</assembly>

File maven-multi-projects/pom.xml

+<?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>multi-modules-project</groupId>
+	<artifactId>mm-parent</artifactId>
+	<version>1.0.0-SNAPSHOT</version>
+	<packaging>pom</packaging>
+
+	<modules>
+		<module>mm-data</module>
+		<module>mm-dist</module>
+	</modules>
+
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+		<spring.version>3.1.2.RELEASE</spring.version>
+		<slf4j.version>1.6.6</slf4j.version>
+	</properties>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<version>2.3.2</version>
+				<configuration>
+					<source>1.6</source>
+					<target>1.6</target>
+				</configuration>
+			</plugin>
+		</plugins>
+	</build>
+
+	<dependencies>
+
+		<!-- Unit testing lib-->
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit-dep</artifactId>
+			<version>4.10</version>
+			<scope>test</scope>
+			<exclusions>
+				<exclusion>
+					<groupId>org.hamcrest</groupId>
+					<artifactId>hamcrest-core</artifactId>
+				</exclusion>
+			</exclusions>
+		</dependency>
+		<dependency>
+			<groupId>org.hamcrest</groupId>
+			<artifactId>hamcrest-library</artifactId>
+			<version>1.2.1</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework</groupId>
+			<artifactId>spring-test</artifactId>
+			<version>${spring.version}</version>
+			<scope>test</scope>
+		</dependency>
+
+		<!-- Logging lib -->
+		<dependency>
+			<groupId>org.slf4j</groupId>
+			<artifactId>slf4j-api</artifactId>
+			<version>${slf4j.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>org.slf4j</groupId>
+			<artifactId>slf4j-log4j12</artifactId>
+			<version>${slf4j.version}</version>
+			<scope>runtime</scope>
+			<optional>true</optional>
+		</dependency>
+		<dependency>
+			<groupId>org.slf4j</groupId>
+			<artifactId>jcl-over-slf4j</artifactId>
+			<version>${slf4j.version}</version>
+		</dependency>
+
+		<!-- Spring context will include core, beans, aop, and expression -->
+		<dependency>
+			<groupId>org.springframework</groupId>
+			<artifactId>spring-context-support</artifactId>
+			<version>${spring.version}</version>
+			<exclusions>
+				<exclusion>
+					<groupId>commons-logging</groupId>
+					<artifactId>commons-logging</artifactId>
+				</exclusion>
+			</exclusions>
+		</dependency>
+		<dependency>
+			<groupId>cglib</groupId>
+			<artifactId>cglib</artifactId>
+			<version>2.2.2</version>
+		</dependency>
+
+	</dependencies>
+</project>

File maven-multi-projects/rename-project.sh

+# This script can help you rename this template project into your own groupId/artifactName
+find . -name "pom.xml" -exec perl -i -pe 's/multi-modules-project/my-project/' {} \;
+find . -name "pom.xml" -exec perl -i -pe 's/mm-/my-/' {} \;
+mv mm-data my-data
+mv mm-dist my-dist