How to create .kt file in generated directory and reference file within project/app? - kotlin

I am generating a class using KotlinPoet and i can write to the generated directory, however I cannot use the class within the project. Is there something I need to add to build.gradle.kts file?

Related

Can't access resources in kotlin from src/main/resources

I have a gradle kotlin project , where I want to load a file from my resources directory.
My kotlin classes are in src/main/kotlin/packagenames and my resources are in src/main/resources
After gradle build including kotlin plugin (kotlin("jvm") version "1.7.20") my classes are copied to build/classes/kotlin/main/packagenames and my resources to build/resources/main
Now I try to read this files from my kotlin code
object {}.javaClass.getResourceAsStream("/resources/filename")
or
object {}.javaClass.classloader.getResourceAsStream("/resources/filename")
but the files are not found.
When I try to see which directory it looks into
println(this.javaClass.getResource("/"))
results in build/classes/kotlin and with javaClass.classLoader it returns null accessing '/'
This means normally I would have to go up two directories and then access resources directory, but this doesn't work as build/classes/kotlin seems to be the root.
Any Idea why this happens.
Remove /resources/ from the input string and instead try
object {}.javaClass.getResourceAsStream("/filename")
This should work as expected.

How to add/import a .kt file as a source in main.kt file in idle kotlin (gradle build) project

I'm learning kotlin recently and have experience with python and pycharm. I'm having some problems on importing a file as a source root in IntelliJ IDE.
my problems are,
If I keep my custom .kt file say calculation.kt in the same folder as main.kt i.e..$project$/src/main/kotlin/main.kt file directory I don't have to import or add calculation.kt in main.kt. All functions within the calculation.kt works fine, maybe being implicitly imported.
But if I keep the file in another folder i.e $project$/src/others/calculation.kt and mark the "others" folder as the source root, yet the calculation.kt doesn't get implicitly imported. Maybe I need to do an explicit import of calculation.kt in my main.kt. I need to know how to do that
Do I have to do any addition to build.gradle.kts file? I think I have to do some inclusion in there, please share an example on that.
Thank you.

IntelliJ file reference file not found

I have been using IntelliJ Idea for a while now, but I have always had a problem with the following:
Show Intention Actions (or Option + Enter) > Inject Language/Reference > File Reference
Whenever I try to use this, it acts like the root directory is the same directory as the Java class. However, the way I have IntelliJ set up is so the root directory for the program is the project directory.
This means that whenever I try to inject a file reference, I can not get the file completion, and I get an error but the code still compiles.
How can I make the file reference feature use the project root for its base directory?

How to mark package as a resource folder?

I have a dir structure for Intellij 12:
...
...test
- java
- com.mycompany.myproject
- package1 (contains code, etc,.)
- resourcePackage (want to contain .json files but can't mark as a resource)
- myOtherJunk.json
- o o o
- resources
- aResource.json
The thing is if I right click on my package name (com.mycompany.myproject) I can only add packages and not directories (like that of an existing resource folder).
However, I don't want to use that existing resource folder for the .json files that I'm going to read into per my test class.
So, I need something to support:
// this already works for the resources directory per the .json file but doesn't for the
// myOtherJunk.json per the resourcePackage.
InputStream is = MyClassTest.class.getResourceAsStream("aResource.json");
This can be solved in several ways. An example of a good approach would be the following folder structure:
src
main
java
resources
test
java
resources
When this is done, you put all you java classes under src/main/java/com.mycompany package and any resources under /src/main/resources/com/mycompany folder.
To link them together, go to the project properties, and find the Path tab. Mark the src/main/java and src/main/resources as source folders. (see the screen-shot attached)
If you link them together, you'll be able to use getResourceAsStream() method.
If you wonder why you should use the following folder structure - this is standard maven way of keeping things clean and tidy.
Directories Creation
Intellij creates directories when you ask her to create package. It is not an error.
If you create package "com", it will create the dir "com", and if you create a source file there, it will think that the file is in the package "com".
If you create package "com.next.pack", it will create three nested dirs "com", then "next", then "pack", and if you create a source file there, it will think that the file is in the package "com.next.pack".
Directories Structures
Only the path under the source root is taken as a package. Any directory(ies) can be set as a source root(s).
Resources roots
Make practically any structure of directories. Somewhere in it there is the root dir of resources. Right-click it and Mark Directory As ... Resources Root.
Notice, the same method can be used for the directories structures for tests, test classes, and test resources. Look here.
Please use #ContextConfiguration annotation to load the resource files. Please see below example.
#ContextConfiguration( { "/app-config.xml", "/test-data-access-config.xml",application-test.yml })

How to Generate a proxy class in a specific project by using svcutil.exe?

By default svcutil.exe generates proxy class and its .config in C:\Program Files\Microsoft Visual Studio 9.0\VC.
What is syntax for generating a proxy class in a specific project by using svcutil.exe ?
Use the /directory option.
To quote directly from the help which is displayed when you type svcutil /? :
/directory:<directory> - Directory to create files in (default: current directory) (Short Form: /d)
Just to elaborate a little more... svcutil just generates the files, it doesn't manipulate the .proj file to add a file to the project, you have to do that yourself. What you might want to do is:
show all files in the Solution Explorer, then refresh the folder containing the generated files, then select them all and add them to the project (you should remove the existing files before doing a manual generation)
write a little batch script or command line app that iterates the folder the files are created in and inserts entries for them into the .proj file.
if possible use the Add Service Reference option from the context menu in the Solution Explorer, that generates the files and adds them to the project for you