2021年使用struts-archetype-blank
Maven Archetype建立Apache Struts 1專案。
範例環境:
- Windows 7 64bit
- jdk 1.8
原本參考封存的Struts 1文件中的Maven指令如下已經失效,第一是因為mvn archetype:create
命令在Maven 3已經廢棄(deprecated)。第二是struts-archetype-blank
這超舊東西已經不存在Maven Central Repository。
$ mvn archetype:create \
-DarchetypeGroupId=org.apache.struts \
-DarchetypeArtifactId=struts-archetype-blank \
-DarchetypeVersion=1.3.5 \
-DgroupId=com.example \
-DpackageName=com.example.projectname \
-DartifactId=my-webapp
然後幸好在網路上找到struts-archetype-blank
的svn source。使用
svn co http://svn.apache.org/repos/asf/struts/maven/trunk/struts-archetype-blank
checkout到本機目錄(本範例為D:\
)。
D:\>svn co http://svn.apache.org/repos/asf/struts/maven/trunk/struts-archetype-blank
移動到checkout回來的struts-archetype-blank
目錄,然後執行mvn install
安裝到Maven Local repository(預設在本機的 ${user.home}\.m2
),${user.home}
是使用者資料夾,例如C:\Users\matt\.m2
。
D:\struts-archetype-blank>mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] --------------< org.apache.struts:struts-archetype-blank >--------------
[INFO] Building Struts Archetypes - Blank 1.3.5-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
...
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ struts-archetype-blank ---
[INFO] Building jar: D:\struts-archetype-blank\target\struts-archetype-blank-1.3.5-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ struts-archetype-blank ---
[INFO] Installing D:\struts-archetype-blank\target\struts-archetype-blank-1.3.5-SNAPSHOT.jar to C:\Users\matt\.m2\repository\org\apache\struts\struts-archetype-blank\1.3.5-SNAPSHOT\struts-archetype-blank-1.3.5-SNAPSHOT.jar
[INFO] Installing D:\struts-archetype-blank\pom.xml to C:\Users\matt\.m2\repository\org\apache\struts\struts-archetype-blank\1.3.5-SNAPSHOT\struts-archetype-blank-1.3.5-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.262 s
[INFO] Finished at: 2021-01-29T16:34:32+08:00
[INFO] ------------------------------------------------------------------------
接著就可以用
mvn archetype:generate -DarchetypeGroupId=org.apache.struts -DarchetypeArtifactId=struts-archetype-blank -DarchetypeVersion=1.3.5-SNAPSHOT -DgroupId=com.abc -DpackageName=com.abc.demo -DartifactId=struts1-demo
指令從struts-archetype-blank
建立Struts 1專案。
D:>mvn archetype:generate -DarchetypeGroupId=org.apache.struts -DarchetypeArtifactId=struts-archetype-blank -DarchetypeVersion=1.3.5-SNAPSHOT -DgroupId=com.abc -DpackageName=com.abc.demo -DartifactId=struts1-demo
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
...
Define value for property 'version' 1.0-SNAPSHOT: : 1.0-SNAPSHOT
[INFO] Using property: package = com.abc
Confirm properties configuration:
groupId: com.abc
artifactId: struts1-demo
version: 1.0-SNAPSHOT
package: com.abc
Y: : y
...
[INFO] project created from Old (1.x) Archetype in dir: D:\struts1-demo
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 05:27 min
[INFO] Finished at: 2021-01-29T16:49:00+08:00
[INFO] ------------------------------------------------------------------------
專案的pom.xml
內容如下。
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>com.abc</groupId>
<artifactId>struts1-demo</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Struts Blank Archetype</name>
<url>http://struts.apache.org/1.x</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts-taglib</artifactId>
<version>1.3.5</version>
</dependency>
<!--dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts-tiles</artifactId>
<version>1.3.5</version>
</dependency-->
<!--dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts-extras</artifactId>
<version>1.3.5</version>
</dependency-->
<!--dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts-el</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.0.4</version>
</dependency-->
</dependencies>
<build>
<finalName>struts1-demo</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<container>
<containerId>tomcat5x</containerId>
<home>c:/java/apache-tomcat-5.5.17</home>
<log>target/tomcat5x.log</log>
<output>target/tomcat5x.out</output>
</container>
<configuration>
<home>target/tomcat5x</home>
</configuration>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.0.1</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
</configuration>
</plugin>
</plugins>
</build>
<!-- Uncomment in order to use Apache's Maven 2 snapshot repository
<repositories>
<repository>
<id>apache.snapshots</id>
<name>Apache Maven Repository (Snapshots and Test Builds)</name>
<url>http://cvs.apache.org/maven-snapshot-repository</url>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
-->
</project>
參考github。
專案建立好後在專案根目錄struts1-demo
輸入mvn jetty:run
即可啟動專案。
D:\struts1-demo>mvn jetty:run
[INFO] Scanning for projects...
[WARNING]
...
[INFO]
[INFO] ------------------------< com.abc:struts1-demo >------------------------
[INFO] Building Struts Blank Archetype 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
...
[INFO] Configuring Jetty for project: Struts Blank Archetype
...
[INFO] Context path = /struts1-demo
...
[INFO] Starting jetty 6.0.1 ...
...
[INFO] Started Jetty Server
[INFO] Starting scanner at interval of 10 seconds.
在瀏覽器輸入http://localhost:8080/struts1-demo
可進入歡迎頁面。
後來我在jdk 11的環境啟動專案並進入頁面時會出現下面錯誤。
org.apache.jasper.JasperException: PWC6033: Unable to compile class for JSP
PWC6199: Generated servlet error:
The type java.lang.String cannot be resolved. It is indirectly referenced from required .class files
PWC6199: Generated servlet error:
java.util.Vector cannot be resolved to a type
PWC6199: Generated servlet error:
_jspx_dependants cannot be resolved
原因應該是使用的Jetty版本太舊導致,把pom.xml
中舊的org.mortbay.jetty:maven-jetty-plugin:6.0.1
改為較新的org.eclipse.jetty:jetty-maven-plugin:9.4.36.v20210114
即可解決。
<!-- <plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.0.1</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
</configuration>
</plugin> -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.36.v20210114</version>
<configuration>
<webApp>
<contextPath>/struts1-demo</contextPath>
</webApp>
</configuration>
</plugin>
新的Jetty預設context path為/
,輸入http://localhost:8080/struts1-demo
會出現HTTP ERROR 404 Not Found頁面,所以要在jetty-maven-plugin
的<contextPath>
設定context path。
沒有留言:
張貼留言