Why does Intellij IDEA say that used properties are unused? - intellij-idea

I have a properties file in my Java maven project. I have one class which loads these properties and stores them as constants.
//Constants.java file has this code.
Properties properties = new Properties(blah...);
String username = properties.getProperty("my.username");
System.out.println(username);
Despite using my.username property and the username variable, Intellij IDEA says that it is unused. Why ? I tried to invalidate caches and restart the IDE, but it did not change anything.
As a result, its hard to find out which properties are actually unused and remove them.
Here is the project structure :
PROJECT1
-> PROJECT2
--> src > main > resources > folder > my.properties
--> pom.xml
-> PROJECT3
--> src > main > java > packagea.b.c > packaged > packagee > Constants.java
--> pom.xml
pom.xml

The issue is not reproducible:
If you have the Minimal, Complete, and Verifiable example, please share it so that we can investigate why the it happens in your specific project.
Here is the properly working sample: unused-properties.zip.
To match your multi module Maven project from the description I've created another sample project to illustrate the configuration and the properly working inspection.
Get the sample project here: unused-multimodule-maven.zip.
This project has an aggregate pom.xml in the root directory and 2 modules: app and properties where app depends on properties.
Proof of working screenshot:
Notice that the app module depends on the properties module. This dependency is achieved via the following pom.xml configuration of the app module.
<dependencies>
<dependency>
<groupId>com.jetbrains.support</groupId>
<artifactId>properties</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

Related

Kotlin/IntelliJ Modules with Shared Content Root

I'm working on a project in IntelliJ Idea using Kotlin. I'm trying to create a unit test for a Kotlin class, and I can create the class, except that when I run the test, I get an error that there is no JDK configured. I go into the project structure and change the JDK for the module, but when I click on Apply, I get a message saying that :
Content root
"C:\Users\2rtmar\Documents\xproject\xproject\examples\src\main" is
defined for modules "xproject-examples-utils_main" and
"xproject-examples-java_main". Two modules in a project cannot share
the same content root.
Another team member had said that these modules were faulty, and that they couldn't be used, but I'm not using them and still prevent me from running my code. I tried to unmark this module as a sources root, but did not fix the problem. I went as far as to removing the xproject\examples module entirely, but received the same message when trying to set the JDK of my module.
Any help is appreciated!
In my case I was trying to setup a multi-module Gradle project by adding a new module to my root Gradle project.
Despite adding an include ':my-submodule' to my settings.gradle, it
was always telling me gradle wrapper not found, because the submodule was kind of outside of the context of the root module.
This however is IntelliJ-specific
When I looked into Right-Click on module -> Open Module Settings - > Modules I was seeing the following hierarchy:
my-root
-> my-submodule
my-submodule
For whatever reason, IntelliJ has added the new module as a child and as a standalone module, and this caused the issue Two modules cannot have the same content root, as my-root:my-submodule and my-submodule are now both pointing to the same src/main directory.
This is IntelliJ's out of the box behaviour when adding a new
submodule, which is in my opinion totally wrong and likely a bug! You
cannot even fix it within the module settings
Solution
But I discovered a way to fix it.
Right-click on the module
Load/Unload Modules
Now unload the module that is not below the my-root
After that, there is just one single root pointing at the src directory and the active module is now within the parent's Gradle scope.
I hope Jetbrains fixes these issues. Those problems exist for a while now and are not pleasant to solve.
I got this issue recently.
The very first check should be to check if any other module with the same content root is added or not.[you can check this in Project Structure -> Modules]
If YES, remove the other project by clicking on - (minus) button at the top.
If you're using Intellij and have modified / are modifying the pom.xml files directly:
Go to the affected sources' pom.xml files
Go to the following area:
...
<build>
...
<resources>
<resource>
...
<directory>[DIRECTORY]</directory>
...
<includes>
...
</includes>
...
</resource>
</resources>
...
</build>
...
Change the [DIRECTORY] to something unique. For instance, I was referencing ../plugin-outputs. As such, I changed it to ../plugin-outputs/[specific sub-directory], wherein I named the sub directory as I wished.
Delete any "modules" listing the specific output directory. No, they aren't modules... however, they have the module icon applied to this, preventing the pom.xml files' changes to be updated while editing modules through the standard project structure tab.
I know this thread is 2 years old; however, there are MANY people who still read these artifacts... hopefully this helps someone.
-Dan

How to refresh lib folder when update pom.xml?

I create a simple sample Spring MVC project, where IntelliJ 14 by default generate a pom with
<properties>
<spring.version>4.1.4.RELEASE</spring.version>
</properties>
I change it to
<properties>
<spring.version>3.2.0.RELEASE</spring.version>
</properties>
and choose Maven -> Reimport, I can see the dependencies are downloaded to my local .m2 folder
However, when I expend lib folder, all dependencies stays with previous version:
How can I get the latest dependencies showing in \lib folder? I tried to synchronize current project, but it doesn't help this matter
UPDATE
here is my maven setting
UPDATE 2
I forget some detail, which is I create a Spring MVC project in the beginning(so I think it may not be a maven project at the moment), then I right click pom.xml and set current project to maven project.
So I think the jar files listed in \lib folder may be downloaeded via intellij for Spring MVC application, however when I set current project to maven project, it does not remove or update the jar file under the \lib folder.
You should do:
1. Choose menu File \ Project Settings..., In section Build, Execution, Deployment \ Build Tools \ Maven \ Importing, check Import Maven projects automatically. It means IntelliJ IDEA will Synchronize Maven project model and IDEA project model each time when pom.xml is changed.
2. Try closing project, restart IntelliJ IDEA, then reopen the project.
3. Check your internet connection.
IntelliJ isn't shouldn't be looking there for your Maven project dependencies. It is should be using the libraries and resources in your .m2 directory instead.
Mind you, I've left those comments struck out on purpose; depending on your configuration, you may accidentally be doing that.
This is a picture of what the Dozer project looks like. It's a Maven project which I cloned a ways back to see how it worked.
You're going to have to check your Project Structure (Ctrl + Alt + Shift + S) to ensure that the libraries that are coming through are prefixed with "Maven:".
If they are, then the files in your lib folder aren't being used by your project.
In all actuality, those are your global libraries (which you can also find under Project Structure > Global Libraries). Any project has access to them.
If that's causing a conflict, consider deleting those JARs from your global libraries. If you need them for another project, consider adding it to the project's local libraries instead.

Auto deploy on save Netbeans entreprise project using maven

I have a NetBeans mavenized project Contaning:
web project (war)
ejb project (jar)
parent project
maven project
After every change I must clean an build the maven project and Run the Parent project
How canI configure my projects to auto deploy on save ?
the configuration file(nb-configuration.xml) is as follows :
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<netbeans.compile.on.save>all</netbeans.compile.on.save>
</properties>
</project-shared-configuration>
right click on your project then select 'properties'. Now, click on 'run' option here select deploy on save. Hope it helps.
Sometimes, restarting NetBeans and server helps to solve autosave problem. It's truly strange, but in my case it helped.

Dependency Management issue in Intellij idea 12.0.4

I am facing a problem modifying dependencies within Intellij12.04.
Under my project folder, I have two modules, the second of which depends on the first:
a.b.c.somebusiness
---> a.b.c.tests
I want to remove one dependency from a.b.c.tests and add it into a.b.c.somebusiness module, but I don't want to create a circular dependency tree. When I try to remove the dependency from the a.b.c.tests module and add it into the a.b.c.somebusiness module, I am getting the following error:
"Source root "C:\Perforce\depot\Projest\Main\a.b.c.tests\src\test" cannot be defined in module "Main" because it belongs to content of nested module "a.b.c.tests""
I don't know what to do to resolve this issue. Can anyone suggest an approach that would fix the error I'm receiving?
You should look at using apache maven and the project object model (pom.xml file)
When you define your dependencies under <dependencies> in the pom.xml, they are managed automatically by IntelliJ. So you will never have such problems from moving dependencies into file folders manually.
If you configure it similar to the fashion shown below, your dependencies will work automagically.
<dependencies>
<dependency>
<groupId>a.b.c</groupId>
<artifactId>a-b-c-tests</artifactId>
</dependency>
<dependency>
<groupId>some.other.org</groupId>
<artifactId>some.other.dep</artifactId>
</dependency>
</dependencies>
How to get started: install apache maven and add a pom.xml at the root of your project. Right click it to add in as a maven file in IntelliJ.
Good luck!
The best approach to isolate issue faced is to use Apache Maven. It gives following advantages and will be easy for you to manage dependencies in a cleaner and efficient way. Hope this will help a new comer.
The task of downloading Jar files and other dependencies is done automatically.
In Maven, it’s easy to add new dependencies by writing the dependency code in the pom.xml file.
Makes it easy for the developer to build a project in different environments without worrying about the dependencies, processes, etc.
Having said that, please follow these steps to fix the issue.
Go to InteliJ IDEA
Right click the Project and Click Add Framework Support and check
the "Maven" option This will create pom.xml file for the project
Double click the pom.xml file and include dependencies as needed.
(The dependencies could be found by searching via
https://mvnrepository.com/)
When the dependencies are added it will automatically download, if
does not, right click the project and select "Maven" and click
"Reimport".
Now you are project is added with the dependencies you have
specified.
Once above is completed, try to build the project in order to deploy/run.
Go to Run -> Edit Configuration -> Add New Configuration -> Select
Maven -> Fill the name etc. -> Type 'clean install' in the 'Command
Line' field and press OK.
Now you can run the Maven in the Run/Debug Configurations
The output can be seen in the target folder of your project
directory.
Check this out for more information - Official Page for Apache Maven:
https://maven.apache.org/index.html

How to copy resources from other module to specific location ? (maven)

I have a maven-managed project with some modules.
One module contains some native codes inside "src/main/resources/native" directory.
Second module packages all related modules to a WAR file.
Here comes the question : How to copy the "native/" directory (and its sub-directories) in first module to WEB-INF/native directory in the second module ?
I found a copy resources plugin , but it seems not what I want. (It copies directory inside the same module , but I want cross-module copy)
This is doable with dependency:unpack (that I would bind on the prepare-package phase) and the appropriate excludes/includes . See the Unpacking specific artifacts example.
The goal of modules in maven is to spearate them from each other. I am afraid there will be no satisfactory solution inside maven as this goes against the grain.
A solution could be to create a war archive with your resources and depend on that to build your final war.
I use for a project for example the camel-web resources by adding a dependency :
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-web</artifactId>
<version>${camel.version}</version>
<type>war</type>
<scope>runtime</scope>
</dependency>
The war resources are merged with my web resources.