Can not resolve configuration property '...
I have no problem accessing my properties through the #Value annotation or through an autowired Evironment. But all of my own defined properties get this warning in IDEA. What should I be doing to get IDEA to recognize these and not bother me?
In order for IntelliJ IDEA to know your Spring Boot properties, you can define Spring Boot configuration metadata in your project.
Option 1:
If you can use a #ConfigurationProperties-annotated class for your properties, you can add the Spring Boot configuration annotation processor to your classpath and IntelliJ IDEA will generate the configuration metadata for you in target or out:
Maven:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
Gradle:
implementation 'org.springframework.boot:spring-boot-configuration-processor'
Option 2:
Create the configuration metadata file yourself src/main/resources/META-INF/spring-configuration-metadata.json:
Content:
{
"properties": [
{
"name": "myapp.someprop",
"type": "java.lang.String"
},
{
"name": "myapp.someintprop",
"type": "java.lang.Integer"
}
]
}
Options 1 and 2:
In the IntelliJ IDEA tool window of your build system (Maven/Gradle), click the "Refresh" button.
Select Build > Rebuild Project from the menu.
If the warning still appears, you can try to restart the IDE. Select File > Invalidate Caches / Restart and click on Invalidate and Restart.
Please use the following for Gradle Kotlin Script for Kotlin project:
plugins {
kotlin("jvm")
kotlin("kapt")
}
/* ... */
dependencies {
val configurationProcessor ="org.springframework.boot:spring-boot-configuration-processor:${BuildConstants.springBootVersion}"
kapt(configurationProcessor) // for jar
kaptTest(configurationProcessor) // for jar
annotationProcessor(configurationProcessor) // for IntelliJ Idea
}
/* ... */
kapt {
annotationProcessor("org.springframework.boot.configurationprocessor.ConfigurationMetadataAnnotationProcessor")
}
/* ... */
tasks {
withType<KotlinCompile> {
dependsOn(processResources)
}
}
Kotlin Kapt is needed to work with metadata and memory.
From official Spring documentation, Spring Boot Configuration Processor generates special json file with properties metadata.
Therefore, to distribute jar with property syntax highlight you need:
Ask Gradle to generate this file
Update task sequence to generate file before jar packaging by using dependsOn (not sure, that my code above is the most effective solution, however problem is solved)
However IntelliJ Idea works with annotationProcessor Gradle configuration (unfortunately, I don't have exact answer, why it requires exact it). Therefore you need add the same processor into the annotationProcessor configuration as well.
I had the same problem plus not showing auto completion found out that it works with IntelliJ Ultimate edition and not community version. link
couple of useful steps to take would be:
adding Maven dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
refresh Maven to download dependencies.
coping exact name of property from target/classes/META-INF/spring-configuration-metadata.js to prevent errors.
making sure that your config class is annotated with #ConfigurationProperties("name-here") and that you have enabled it by #EnableConfigurationProperties(NameOfTheConfigClass.class)
A Gradle-based workaround is this:
afterEvaluate {
val kaptKotlinTasks = tasks.named("kaptKotlin") {
doLast {
val kaptKotlin = this
tasks.named<ProcessResources>("processResources") {
from(kaptKotlin.outputs) {
include("META-INF/spring-configuration-metadata.json")
}
}
}
}
tasks.named("processResources") {
this.dependsOn(kaptKotlinTasks)
}
}
After running a build (or just the processResources task) from the Intellij Gradle panel, the warnings about the properties should disappear.
Not ideal, but IntelliJ not supporting kapt is not ideal either :-/
As an additional requirement for the answers above. After Spring-boot 2.2 you can use final keyword on the attributes together with the annotation #ConstructorBinding in order to see IntelliJ auto-complete the property name on the application.properties file. IntelliJ also recognizes if you add a java docs on the attribute.
#ConfigurationProperties(prefix = "my-config")
#ConstructorBinding
public class ConfigImportService {
/**
* the name of the bucket
* (IntelliJ shows this comment on the application.properties file)
*/
private final String bucketName;
private final String databaseName;
#Autowired
public ConfigImportService(
#Value("${bucket.name}") String bucketName,
#Value("${db.name}") String databaseName
) {
this.bucketName = bucketName;
this.databaseName = databaseName;
}
}
And still necessary the dependency, of course.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
I have multiple POMs that inherit a dependency via their parent. The dependency is not to a maven plugin, but another project that I have created:
<dependency>
<groupId>com.my.name.group</groupId>
<artifactId>id-of-my-artifact</artifactId>
<version>2.3.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
Now I want to remove this from the parent POM file and instead have the children POMs use a custom maven plugin for the same functionality. In that custom maven plugin (which I will create), I want to call/add above dependency. I have tried the following code in a Mojo of the new maven-plugin, but it is not adding the dependency (and gives compilation error):
private MavenProject project;
public void execute() throws MojoExecutionException, MojoFailureException {
try {
// adding a dependency
Dependency dep = new Dependency();
dep.setArtifactId("id-of-my-artifact");
dep.setGroupId("com.my.name.group");
dep.setScope("compile");
dep.setVersion("2.3.0-SNAPSHOT");
List<Dependency> lst = new ArrayList<Dependency>();
lst.add(dep);
project.setDependencies(lst);
} catch (Exception ex) {
getLog().info(String.format("Exception %s: ", ex));
throw new MojoExecutionException("Exception has occurred" + ex);
}
}
Is it possible to do this? If yes, how?
I looked at this related question and the link provided in that. From the external link, it seems that it is not possible. But in that question, OP seems to have figured it out and provided a short answer, but I cannot understand the answer. Could someone please help me out? I am new to Maven.
We have an existing JavaEE web application. We need to connect to a document repository via webdav. I am trying to use sardine but the couldn't find clear setup instructions. Is the Maven build mandatory? Or, is it possible to simply include sardine.jar in our application and use the below code?
Sardine sardine = SardineFactory.begin("username", "password");
List<DavResource> resources = sardine.list("http://myhost.com/modeshape-webdav/sample/default");
for (DavResource res : resources)
{
System.out.println(res);
}
If you use Maven, you can add this dependency in your pom.xml to use Sardine :
<dependency>
<groupId>com.github.lookfirst</groupId>
<artifactId>sardine</artifactId>
<version>5.5</version>
</dependency>
Can any one suggest how to resolve the ClassCastException while adding dependency jar in pom.xml?
I have created a new maven project having returning the object in runtime and then execute the ovverride method definition as based upon client request.
Ex: actionPerform(actionType) -- this method return the 'Object'
public static CommonActions action(ActionTypes actionType)
{
return (CommonActions)actionPerform(actionType);
}
Code is working fine and then successfully executing. There is no ClassCastExceptions while adding this jar into library tab on project properties using eclipse.
Getting ClassCastExceptions on above return statement while adding the below depenedency in pom.xml
<dependency>
<groupId>com.amadeus.selenium.merci</groupId>
<artifactId>appium-amadeus-utilities</artifactId>
<version>1.4-SNAPSHOT</version>
<scope>runtime</scope>
</dependency>
I am using maven version 3.0.4
Please suggest how I resolve the ClassCastException at runtime while adding dependency jar in pom.xml.
I have installed the Grails Fixtures plugin (http://www.grails.org/plugin/fixtures) for loading some initial datas into my database for dev and test environment. I also use Grails with Maven integration.
I have added my data loading code into the BootStrap.groovy:
import grails.util.Environment
class BootStrap {
def fixtureLoader
def init = { servletContext ->
if (Environment.current == Environment.DEVELOPMENT || Environment.current == Environment.TEST) {
//def fixtureLoader = new FixtureLoader(grailsApplication)
fixtureLoader.load("init")
}
}
}
When I run my Grails app with "grail run-app" it works perfectly, but if I use the Maven Grails command "mvn grails:run-app -Dgrails.env=development" then it doesn't work. It throws following error:
Error executing bootstraps; nested exception is java.lang.NullPointerException: Cannot invoke method load() on null object
It seems that the "fixtureLoader" bean is not correctly initialized if I use the Maven Grails command "mvn grails:run-app".
Do you have any idea? Or maybe its a bug...
Add it as a dependency in pom.xml instead of BuildConfig.groovy. Maven looks at the pom to resolve dependencies (in this case a plugin).
<dependency>
<groupId>org.grails.plugins</groupId>
<artifactId>fixtures</artifactId>
<version>1.0.7</version>
<scope>runtime</scope>
<type>zip</type>
</dependency>
Note: scope runtime makes the artifact available in test scope as well.