Monday, September 12, 2016

spring 4.2.x with cross domain profile

1.what
i need write cross domain profile in my java server with spring.

2.how
after spring web 4.2.2,can simple write below code in spring-mvc.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns:mvc="http://www.springframework.org/schema/mvc"  
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd>
      <!-- other code -->
     <mvc:cors>
<mvc:mapping path="/**" allowed-origins="*"
allowed-methods="*"
allowed-headers="*"
exposed-headers="X-Requested-With, X-PKRSS-Token" allow-credentials="true"
max-age="36000" />
</mvc:cors>
</beans>

x.Last
Thanks readed~_~

tomcat manager with eclipse maven deploy profile

1. what
my java web server is tomcat 9 m9,with tomcat manager,i can deploy web app war with eclipse maven menu,or by tomcat manager web,or by script.

2. how
2.1 install tomcat 9

# yum install -y java wget

# mkdir ~/download
# cd ~/download
# wget -c http://mirrors.cnnic.cn/apache/tomcat/tomcat-9/v9.0.0.M9/bin/apache-tomcat-9.0.0.M9.tar.gz
# tar -zxvf apache-tomcat-9.0.0.M9.tar.gz
# mv apache-tomcat-9.0.0.M9 /usr/local/tomcat9

# vi /etc/profile

export TOMCAT_HOME=/usr/local/tomcat9
export CATALINA_HOME=/usr/local/tomcat9


# source /etc/profile

(6)
# vi /usr/local/tomcat9/conf/tomcat-users.xml

<role rolename="tomcat"/>
<role rolename="manager-script"/>

<user username="manager" password="123456" roles="manager-gui"/>
<user username="script" password="123456" roles="manager-script"/>


# mkdir -p /usr/local/tomcat9/conf/Catalina/localhost/
# vi /usr/local/tomcat9/conf/Catalina/localhost/manager.xml

<Context privileged="true" antiResourceLocking="false"
         docBase="${catalina.home}/webapps/manager">
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow=".*" />
</Context>

# only for my project profile, every can skip this.

# vi /usr/local/tomcat9/conf/server.xml
replace:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
with:
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000" useBodyEncodingForURI="true" URIEncoding="UTF-8"

# vi /usr/local/tomcat9/conf/catalina.properties
shared.loader=${catalina.base}/shared/*.jar

# vi /usr/local/tomcat9/conf/logging.properties

1catalina.org.apache.juli.AsyncFileHandler.level = OFF
2localhost.org.apache.juli.AsyncFileHandler.level = OFF
java.util.logging.ConsoleHandler.level = OFF
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = WARN

# mkdir /usr/local/tomcat9/shared/
upload our project's lib to this folder.

# only for my project lib, every can skip this.
copy mapper-x.x.x.jar mysql-connector-java-x.x.x.jar /usr/local/tomcat9/lib/

# vi /etc/init.d/tomcat

#!/bin/bash
#
# ldl 120449427@qq.com
# /etc/rc.d/init.d/tomcat
# init script for tomcat precesses
#
# processname: tomcat
# description: tomcat is a j2se server
# chkconfig: 2345 86 16
# description: Start up the Tomcat servlet engine.

if [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
else
echo -e "/atomcat: unable to locate functions lib. Cannot continue."
exit -1
fi

RETVAL=$?
CATALINA_HOME="/usr/local/tomcat9"

case "$1" in
start)
if [ -f $CATALINA_HOME/bin/startup.sh ];
then
echo $"Starting Tomcat"
$CATALINA_HOME/bin/startup.sh
fi
;;
stop)
if [ -f $CATALINA_HOME/bin/shutdown.sh ];
then
echo $"Stopping Tomcat"
$CATALINA_HOME/bin/shutdown.sh
fi
;;
restart)
if [ -f $CATALINA_HOME/bin/shutdown.sh ];
then
echo $"Stopping Tomcat"
$CATALINA_HOME/bin/shutdown.sh
fi
if [ -f $CATALINA_HOME/bin/startup.sh ];
then
echo $"Starting Tomcat"
$CATALINA_HOME/bin/startup.sh
fi
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
;;
esac

exit $RETVAL

# chmod 755 /etc/init.d/tomcat

chkconfig tomcat on
# chkconfig --add tomcat

# chown -R ec2-user /usr/local/tomcat9/webapps

== crontab -e
0 0 * * * rm -rf /usr/local/tomcat9/logs/*20*


# by new version cent  os firewall,not for iptables
firewall-cmd --permanent --add-port=80-80/tcp
firewall-cmd --permanent --add-port=843-843/tcp

firewall-cmd --permanent --add-port=8080-8081/tcp

2.2 setting for maven profile pom.xml
<project>
     <!-- ... -->
<profiles>
<profile>
<id>b</id>
<build>
<finalName>ROOT</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>log4j.properties</exclude>
<exclude>local.properties</exclude>
</excludes>
</resource>
<resource>
               <directory>src/main/profile/b</directory>
<includes> 
<include>log4j.properties</include>
<include>local.properties</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>${tomcat7-maven-plugin.version}</version>
<configuration>
<url>http://my server ip/manager/text</url>
<path>/ROOT</path><!-- ##${maven.build.timestamp} --><!-- Tip: your can deploy multiple version with same app by tomcat support with timestamp version. -->
<username>script</username>
<password>123456</password>
</configuration>
</plugin>
</plugins>
</build>
</profile>

</profiles>
</project>

2.3 run as maven right menus:
Eclipse > project explorer > Run as > Run configurations > Maven build > new:
Name: clean install b
Base directory: your project directory
Goals: clean install
Profiles: b


It means shell: $ mvn clean install -P b

Eclipse > project explorer > Run as > Run configurations > Maven build > new:
Name: tomcat7 redeploy
Base directory: your project directory
Goals: tomcat7:redeploy

Profiles: b

It means shell: $ mvn tomcat7:redeploy -P b

2.4 run

Eclipse > project explorer > Run as > Maven build > clean install b
Eclipse > project explorer > Run as > Maven build > tomcat7 redeploy

or


Eclipse > project explorer > Run as > Maven build > clean install b

web browser access: http://my server ip/manager/html/ ,type with username: manager password: 123456

Select WAR file to upload: your project directory/target/ROOT.war
Click Depoly.

X Last
Thanks~_~


Tuesday, May 10, 2016

jekins+tomcat7+maven+auto deploy+junit+svn developer log

1. what
 i need used maven to auto deploy my spring war to tomcat with version war. it can auto run junit,if ok, then publish it to tomcat,else email me error history.

2. how 2.1 install tomcat 7+

edit tomcat\conf\tomcat-users.xml

<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="admin-gui"/>
<user password="tomcat" roles="manager-gui,admin-gui" username="tomcat"/>
<user password="123456" roles="manager-script" username="admin"/>

2.2. edit spring web project's pom.xml

<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>com.pkrss</groupId>
  <artifactId>test1</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>test1 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <properties>
<project.sourceEncoding>UTF-8</project.sourceEncoding>

<tomcat7-maven-plugin.version>2.2</tomcat7-maven-plugin.version>

<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
</properties>

  <dependencies>
    <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
  </dependencies>
  
  
  <build>
    <finalName>${project.artifactId}</finalName>
    
  
<directory>target</directory>
       <sourceDirectory>src/main</sourceDirectory>
       <testSourceDirectory>src/test</testSourceDirectory>
       <outputDirectory>target/classes</outputDirectory>
       <resources>
           <resource>
               <directory>src/main/resources</directory>
               <includes> 
<include>**/*</include> 
</includes>
           </resource>
       </resources>
  </build>
  
    
  <profiles>
        <profile>  
            <id>ldl</id> 
            <build> 
                <plugins>  
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>${tomcat7-maven-plugin.version}</version>
<configuration>  
                            <url>http://127.0.0.1:8080/manager/text</url>  
                            <path>/${project.artifactId}##${maven.build.timestamp}</path>
                            <username>admin</username>  
        <password>123456</password>
                        </configuration>  
</plugin>
                </plugins>
            </build>  
        </profile>
    </profiles>

</project>

2.3. install jekins in tomcat
used sve SCM
Poll SCM: H/5 * * * *
Build: mvn clean test tomcat7:redeploy -P ldl
Publish JUnit test result report: **/target/surefire-reports/*.xml

E-mail Notification
Recipients:  120449427@qq.com

Saved


2.4 ok