问题
In my current state of the pom.xml i have
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<path>/myPath</path>
</configuration>
</plugin>
this is working fine. I now am trying to add an extra context for the images on the server,
so that the "old" context /myPath
is directing to the webapp and /images
can be used to address images.
I have tried to add a context file, but when I do this only my /images
context is loaded.
Also, I don't want to build the WAR each time (this would be the case if I used two context.xml files for the two contexts)
Is it possible to add a) the context as it is (the current state of development using tomcat7:run AND b) the second context /images that only points to a local folder? And how?
回答1:
Although it's answered here, thought it would be good to post how the xml should look like:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<path>/myPath</path>
<staticContextPath>/images</staticContextPath>
</configuration>
</plugin>
The other answer uses staticContextDocbase instead of staticContextPath, and I can't tell the difference between the two, but one of then should work. Haven't tried it myself, though ;)
Tomcat's doc of these two properties:
staticContextDocbase:
The static context docroot base fully qualified path
staticContextPath:
The static context
Could be that fully qualified path
is in contrast to relative path.
Well, I delved a bit into the plugin and Apache's code and found that you need both staticContextDocbase
and staticContextPath
.
staticContextDocbase
is the path from where the static context should be retrieved by Tomcat. In your case it is C:/images
.
staticContextPath
is the part in the URL after the http://<hostname>:<port>
for which the static context should be sent to client. In your case it is /images
.
Maven should be configured like so:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<path>/myPath</path>
<staticContextPath>/images</staticContextPath>
<staticContextDocbase>C:/images</staticContextDocbase>
</configuration>
</plugin>
Another note:
As seen here, the plugin uses Tomcat.addContext(String contextPath, String baseDir), staticContextPath
is passed as contextPath
and staticContextDocbase
is passed as baseDir
. The doc for baseDir
states that it Must exist, relative to the server home
.
OTOH, that baseDir
is moved as is to Context.setBaseDir(String docBase). The doc on that method for baseDir
states that This can be an absolute pathname, a relative pathname, or a URL.
.
Try the full path then. If it doesn't work go for the relative ;).
来源:https://stackoverflow.com/questions/17674519/tomcat-7-maven-plugin-multiple-context