IntelliJ: Invalid .iml location and content when I choose "create new project" over "open existing project" - maven

I have several question about how the project structure is created and stored. Here is the context: I have a git repository containing some groovy files which I use for a Jenkins library. So what is interesting us here is the "src" folder, which contains groovy classes, and the "vars" folder, which contains groovy scripts (DSL).
There's no existing IntelliJ project, no .idea folder, no .iml, no pom.xml ...
Let's say that the repository is located under:
C:\Toto\JenkinsLibrary
- .git
- src
- vars
My objective is to create a Maven project from scratch.
First Method:
Initially, I open IntelliJ and choose "Create new project". Select "Maven", no archetype, then I enter the following information:
Project name: I leave the project corresponding to the artifactID: JenkinsBuildLibrary
Project location: C:\Toto\JenkinsLibrary
More settings: Module name = JenkinsLibrary, Content root = Module file location = C:\Toto\JenkinsLibrary, Project format = .idea
Then click finish
Here, the .iml file is generated OUTSIDE the .idea folder, looking like this:
C:\Toto\JenkinsLibrary
- .git
- .idea
- src
- vars
- JenkinsLibrary.iml
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Then soon after the content of the iml goes to:
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />
Also, I have been trying to reproduce this several times and it is extremely complex to get a consistent behavior, sometimes I have the iml file with no generated pom, sometimes I have the pom with no iml .... so I guess there's a huge bug here.
Finally, if I change the project structure by adding other sources folder or other modules, nothing changes in my .iml file. Then I can close IntelliJ, remove ALL .idea, .iml and pom.xml files ... to get everything back once I reopen IntelliJ, leaving me to wonder: where is that configuration actually stored ?!
Second method:
This time, I choose "Open existing project". I select the folder: C:\Toto\JenkinsLibrary
There's no existing project but it still works and create a new project.
The .iml file is now under the .idea project !!
C:\Toto\JenkinsLibrary
- .git
- .idea
- JenkinsLibrary.iml
- src
- vars
- pom.xml
Right click the module, select "Add Framework Support", Maven, and I get a pom file.
When I change the project structure, the .iml is correctly updated. Exemple:
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<content url="file://$MODULE_DIR$/../../tools/ToolJenkins/src">
<sourceFolder url="file://$MODULE_DIR$/../../tools/ToolJenkins/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Now If I close IntelliJ, remove ALL .idea, .iml and pom.xml files ... I don't get anything back once I reopen the project (only the files and folder located under the module), which seems a far better behavior !
SHORT SUMMARY:
When creating a new Maven project, the .iml is located at the root of the project instead of under the .idea folder and is invalid. The project structure seems to be stored elsewhere. I have no way to ship a valid project to my teammates.
When opening a new project, the .iml is located under the .idea folder and is valid. The project structure is stored under it (with warnings that project are defined by a pom file and changes performed under the Project structure menu could be wiped). I can ship a valid project to my teammates.
I need to understand those differences and how IntelliJ works. I hope for a quick answer since this is not the first time we struggle with project structures and may well move back to another, more stable IDE is necessary.
Thanks !

IntelliJ IDEA stores generated project files externally for Maven or Gradle based projects. You should never edit them manually or adjust any content roots and source roots via the interface as they are managed by the build system configuration (pom.xml) in your case.
To change or add additional source roots you need to modify pom.xml directly.
It looks like your directory layout doesn't follow the standard Maven convention. You may need to override it or add additional directories.

Related

WebAPI Core, set a custom nuget/packge folder

I am trying to figure out how to override the package folder from the user folder to a custom folder location like inside my project folder
I have a webapi Core project with an appSetting.json
I read a few posts and may suggests using nuget.Config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<clear />
<add key="globalPackagesFolder" value="d:\\packages" />
</config>
</configuration>
I added in my WebAPI project which show along side with appsetting.json.
When I build the project, I see no package folder.
I also read about packages.config but I can't find that file anywhere.
Any help is appreciated.
Thanks
The nuget.config file should be placed in a directory one level above the project and then it will work.
For an example, the ConsoleApp152 folder is the folder which contains all your project files. And nuget.config file should be placed at the one level above the project files.
When you finish it, please close VS and then restart your project to enable this setting. And it will act on all projects in the directory level where the current nuget.config file is located and also all projects in the subordinate directories.

Automatically add project dependency metadata to manually created nuspec file

When I nuget pack a web project I want to specify custom unpack locations for content and maintain project dependency metadata.
Given the following manually created example nuspec file:
<?xml version="1.0"?>
<package>
<metadata>
<id>Web.MyApp</id>
<version>1.0</version>
<title>Web.MyApp</title>
<authors>Chris</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Nuget package containing files for Web.MyApp</description>
<releaseNotes>release notes</releaseNotes>
<copyright>Copyright Chris 2017</copyright>
<tags />
<dependencies />
</metadata>
<files>
<file src="bin\**\*.*" target="bin" />
<file src="views\**\*.*" target="views" />
<file src="content\" target="content" />
<file src="scripts\" target="scripts" />
<file src="Global.asax" target="" />
<file src="*.config" target="" />
</files>
</package>
This allows me to specify custom unpack locations for bin, content, sprint folders etc but I want the project dependency metadata to be automatically maintained. I don't want to edit the nupsec each time a new dependency is referenced.
As an attempt to resolve this problem I tried to nuget pack the csproj file instead of the nuspec. This maintained the dependency metadata however it made specifying content unpack locations much trickier. I can do the following:
<Content Include="Content\dist\images\brand-logo.svg">
<Pack>true</Pack>
<PackagePath>Content\Content\dist</PackagePath>
</Content>
But I couldn't find an elegant solution for the bin folder. I just feel like I'm doing something fundamentally wrong.
So my question is, how can I automatically maintain project dependency metadata when creating a nuget package from a manually created nuspec file?
The pack command I am using:
..\tools\nuget\nuget.exe pack $project + ".nuspec" -IncludeReferencedProjects -
Properties Configuration=Release -Verbosity quiet -NonInteractive -
OutputDirectory "$packagedirectory" -Version $buildVersion -Symbols
Automatically add project dependency metadata to manually created nuspec file
If I understand you correct, I am afraid you have already automatically add project dependency metadata to manually created .nuspec file. You just need to rebuild the project and re-pack the .nuspec file.
When you include the referenced files with wildcard, it will contain the new added project references:
<file src="bin\**\*.*" target="bin" />
Add a new project reference to the project, then re-build the project, the dll file of referenced project will be copied to the \bin folder, So we just need to re-pack the .nuspec file, the referenced project metadata will included in the new created package.
For example, add a Atest reference project to Web.MyApp project, then rebuild the project and re-pack the .nuspec file:
If I misunderstand you, please let me know for free.

Unable to Create .go File Within IntelliJ IDEA

I'm using Intellij IDEA Community 2016.3 with the golang plugin.
Recently within Intellij I created a new package then I tried to create a new go file in that package. Intellij gave me a template error message, something like this:
Unable to parse template "Class" Error message: This template did not produce a Java class or an interface - during New -> Java Class
(To create the file I right-clicked the package and selected New > Go File.)
I then went into the file system and manually created the file. After a brief pause, Intellij closed the pane on the left that lists all my packages. Intellij also corrupted the project such that I was not able to view the project pane again.
I recreated the project and everything seems to be fine now but I don't want to have to do all that again each time I create a .go file.
I don't know if this makes a difference, but the package I tried creating the file in is a download from github. Intellij has been telling me this:
The directory C:\go-workspace\src\gopl.io is under Git, but is not registered in the Settings.
It's someone else's project -- files for learning go-- and I don't plan on sending any pull requests. I simply created the package and file there because they are part of learning go.
How do I create .go files in Intellij?
Added 21-Jan:
Here are my project settings:
Make sure you got the Go setup properly before continuing.
Next I'd also make sure the plugin setup is good(maybe reinstall after making sure your go setup is ok), such as the project SDK is set to something Go and not Java as that's usually the default(see image below) and IntelliJ can detect your GOPATH.
If the project your'e working on was not an IntelliJ module before and you just opened it, make sure the .iml file is a Golang module (IntelliJ usually asks you to do this automatically if it's not already done, but you need to setup the project SDK to Go first I assume)
ie.(my hello.iml);
<?xml version="1.0" encoding="UTF-8"?>
<module type="GO_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="GOPATH <hello>" level="project" />
</component>
</module>
Here is a comprehensive step by step guide for a starter setup;
https://rootpd.com/2016/02/04/setting-up-intellij-idea-for-your-first-golang-project/

Making nuget work when its referenced in multiple solutions or by itself?

We have the following project structure in a solution:
.\Project2
.\Project1
Project2 is dependent on Project1. Project1 is shared across multiple projects. I added the nuget packages (Roslyn for example) for Project1 when it was part of this solution. The problem we are having if either we try to compile Project1 by itself or compile it when its in an entirely another app it fails:
This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is ../../Project2/packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props.
If i open the project file in a text editor it has the following statments in the project file:
<Import Project="..\..\Project2\packages\Microsoft.Net.Compilers.1.2.2\build\Microsoft.Net.Compilers.props" Condition="Exists('..\..\Secured Account Access\Member\packages\Microsoft.Net.Compilers.1.2.2\build\Microsoft.Net.Compilers.props')" />
If of course removed all the nuget references and then just opened the individual project and added the nuget references. Which fixed when i was trying to compile the project by itself and broke it when it was part of the solution because VS will no longer restore the packages for Project1 while its in the solution.
I'm not sure why VS/Nuget works this way. Why not always have a package folder for each project? Disk Space savings (which would be absolutely nothing)? You should never have references that look like the following:
../../Project2/Packages/MyNugetDepedency.dll
Is there any way to get around this and have everything just work.
The packages folder is determined by the solution the project is contained in by default. You can override the default packages directory by specifying the repositoryPath in a NuGet.Config file.
<configuration>
<config>
<add key="repositoryPath" value="../MyPackages" />
</config>
</configuration>
To have multiple solutions all share the same packages directory you can create a NuGet.Config file, with a repositoryPath setting, in a directory that is a parent to all the solutions. NuGet will then work its way up each directory until it finds a NuGet.Config file and then use the repositoryPath defined there. This repositoryPath is relative to the NuGet.Config file itself, unless you have specified a full path. A relative path is probably what you want so other developers do not need to check out the source code to the same directory.

Is there a way to add source files to visual studio project from command-line?

I want to use sublime to edit a visual studio project.
I have a custom build:
{
"cmd": ["c:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild.exe"],
"working_dir": "${project_path:${folder:${file_path}}}/../Project"
}
But if I add new files I also need to include them in the project.
Is there a way to do this from the command line, maybe at compile-time?
I am working with opengl using c++;
I basically set up a project using one of the examples provided on the opengl website.
Then I opened the project folder in sublime text and successfully compiled it using the custom build system.
However, when I add NEW source files to the project (*.h and *.cpp) I get a linking error.
I get the same error when I build in visual studio.
The error disappeared after I had included the files by manually browsing and adding them to the project.
What I wanted was a way to automatically add all the source files in a folder to the project(via command line, or wildcard or smth else).
This way I can easily work on a vs2010 project in sublime, add new source files and build the project.
Or maybe there already is a better workflow for this?
You could try to modify your .vcxproj file to include any .h and .cpp file in your project folder or folders below.
In case of a c++ VS project you can try to alter your .vcxproj file like this:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- rest of project file untouched -->
<!-- start of modified part -->
<ItemGroup>
<ClInclude Include="**\*.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="**\*.cpp" />
</ItemGroup>
<!-- end of modified part -->
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Be aware that adding files to your project from inside VS at later point will replace the modification described above!
As an alternative you could also create an external project file holding the same <ItemGroup /> elements described above and include this project file into your .vcxproj.
I'll add an example of this alternative if you're interested.

Resources