Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a value in web.xml with a Maven property?

I have a Maven project that downloads some test files into its build directory ./target/files. These files should then be available to a servlet, which I can easily achieve by hardcoding the full path as an <init-param> of the servlet:

<servlet>     <servlet-name>TestServlet</servlet-name>     <servlet-class>my.package.TestServlet</servlet-class>     <init-param>         <param-name>filepath</param-name>         <param-value>/home/user/testproject/target/files</param-value>     </init-param> </servlet> 

How can I avoid hardcoding the full path and use a dynamic parameter replacement instead? I tried the following, but it did not work:

<param-value>${project.build.directory}/files</param-value> 
like image 865
dokaspar Avatar asked Mar 12 '13 07:03

dokaspar


People also ask

Is it possible to refer a property defined in your pom XML file?

User-defined properties can be referenced in a POM, or they can be used to filter resources via the Maven Resource plugin.

What is properties in Maven POM XML?

Maven properties are value placeholders, like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}, where X is the property. Or they can be used by plugins as default values, for example: In your case you have defined properties as version of java.

How to specify servlet in web xml?

Configuring and Mapping a Servlet This is done using the <servlet> element. Here you give the servlet a name, and writes the class name of the servlet. Second, you map the servlet to a URL or URL pattern. This is done in the <servlet-mapping> element.

What is servlet tag in web xml?

The <servlet> element declares the servlet, including a name used to refer to the servlet by other elements in the file, the class to use for the servlet, and initialization parameters. You can declare multiple servlets using the same class with different initialization parameters.


1 Answers

Add to your pom section:

<plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-war-plugin</artifactId>     <configuration>         <webResources>             <resource>                 <filtering>true</filtering>                 <directory>src/main/webapp</directory>                 <includes>                     <include>**/web.xml</include>                 </includes>             </resource>         </webResources>         <warSourceDirectory>src/main/webapp</warSourceDirectory>         <webXml>src/main/webapp/WEB-INF/web.xml</webXml>     </configuration> </plugin> 

See Maven: Customize web.xml of web-app project for more details

like image 98
Andrzej Jozwik Avatar answered Oct 06 '22 22:10

Andrzej Jozwik