Reputation: 11
I needed to create a single executable jar from my maven module (Java+Spring) and its dependencies. So I included the maven-shade-plugin in my POM, and looks like it packaged all that was needed.
When I run this jar, it fails with a NullPointerException. It looks like it fails to locate the values referenced by @Value in my main class, and I end up getting a NPE. I do see that the applicationContext and properties files are inside the jar. What might be the reason for this?
applicationContext.xml -
<context:property-placeholder location="classpath:props.properties" ignore-unresolvable="true"/>
Main.java -
@Value("${property.inside.props.file}")
private String propertyName;
pom.xml -
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.sloan.Main.java</Main-Class>
</manifestEntries>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
I have tried doing the same thing with the assembly plugin, but that brought up the NPE as well.
Upvotes: 0
Views: 332
Reputation: 11
I found what I was missing, loading the Spring context! So now my Main.java loads the context first thing -
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("META-INF\\spring\\applicationContext.xml");
Things are working after making this change and reinstalling.
Upvotes: 1
Reputation: 525
Your are missing a @Bean
that is responsible for resolving this annotations
Add this bean to your configuration class
@Bean
public static PropertyPlaceholderConfigurer getValueResolver()
{
return new PropertyPlaceholderConfigurer();
}
for XML based tutorial look here http://www.mkyong.com/spring/spring-propertyplaceholderconfigurer-example/
Upvotes: 0