Remote deployment of maven project using tomcat configuration database connection method
- 2021-08-17 01:34:23
- OfStack
1. Remote deployment using tomcat
1.1 Problems encountered:
The third party jar package needs to be referenced in the project. When using mvn to package the project, an error will be reported
'dependencies.dependency.systemPath'for com.dingtalk.api:taobao-sdk-java:jar must be omitted.
The reason for the problem is that mvn loads pom. xml first when packaging the project, and an error will be reported if the dependency is not available in the local warehouse.
Solution: Install the third party jar package to the local repository using the command: Mvn install: install-file-Dfile=E:\ taobao-sdk-java-auto_1479188381469-20200422.jar-DgroupId=taobao-sdk-java-DartifactId=taobao-sdk-Dversion=2.0-Dpackaging=jar Add war to the pom dependency and type the project as war package, otherwise it will default to jar package After configuring tomcat-users. xml in the tomcat config directory, restart the tomcat service if it is not possible to restart tomcat. If you can successfully access http: //localhost: 8080/manager/html, To prove that the configuration is successful, it is necessary to quickly locate the cause of the error through tomcat log information. Improve the efficiency of problem solving 5. The reason why the previous attempt was unsuccessful is that this project is a copied project, the path of terminal command line in idea is still the path of the previous project, and pom in the previous project has no plug-in configured, so it cannot be connected.
1.2 Configuration for Remote Deployment
(1) Add plug-ins:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<username>admin</username>
<password>123456</password>
<url>http://47.102.123.186:8095/manager/text</url>
<server>tomcatServer</server> // To be associated with server In id Keep 1 To
<update>true</update>
<path>/zw</path>// Project name
</configuration>
</plugin>
(2) Configure the tomcat-users. xml configuration file under the tomcat directory:
<role rolename="admin-gui"/>
<role rolename="manager-gui"/> // Allow access html Interface ( I.e. URL The path is /manager/html/*)<role rolename="manager-script"/>// Allow access to plain text interfaces ( I.e. URL The path is /manager/text/*)
<user username="admin" password="123456" roles="admin-gui,manager-gui,manager-script"/>
// Attention! You can add multiple roles to this user, and at least this role is required for remote deployment: manager-script, It can also be turned on manager-gui Used for visual management
(3) Configure the setting. xml file for maven under the servers tab:
<server>
<id>tomcatServer</id>
<username>admin</username>
<password>123456</password>
</server>
(4) Use command: mvn tomcat7: deploy
2. Steps to configure database connection configuration using tomcat's configuration file
You need to configure server. xml, context. xml, web. xml under the conf directory of tomcat.
The specific configuration is as follows: Server. Configuration under GlobalNamingResources tag of xml:
`<Resource name="jdbc/DataSource" auth="Container"
type="javax.sql.DataSource"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://192.168.0.1;databaseName=xydi" username="sa"
password="sa"
maxActive="20" // Maximum number of connections
maxIdle="10"
maxWait="-1"/>`// Maximum waiting time
Configuration under Context. xml:
<ResourceLink name="jdbc/DataSource" global="jdbc/DataSource"
type="javax.sql.DataSource"/>// Introduce server.xml Configuration information for
Configuration under Web. xml:
<resource-ref>
<res-ref-name>jdbc/DataSource</res-ref-name>// And resource Adj. name Keep 1 To
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>`
If you are using the spring framework:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/DataSource"/>
<property name="expectedType" value="javax.sql.DataSource"/>
</bean>
The spring framework is not used:
Connection con = null;
Context c = new InitialContext();
DataSource ds = (DataSource) c.lookup("java:/comp/env/jdbc/DataSource");// The project name should be the same as the context.xml Correspondence inside
con = ds.getConnection();
Summarize